Debian Codegraph @

src/services/castletenjin.pas

raw
1 {
2 Copyright 2021-2023 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Tenjin integration (TCastleTenjin). }
17 unit CastleTenjin;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses Classes;
24
25 type
26 { Tenjin ( https://www.tenjin.com/ ) integration.
27 On Android ( https://github.com/castle-engine/castle-engine/blob/master/tools/build-tool/data/android/integrated-services/tenjin/README.md )
28 and on iOS ( https://github.com/castle-engine/castle-engine/blob/master/tools/build-tool/data/ios/services/tenjin/README.md ).
29
30 Usage:
31
32 @orderedList(
33 @item(Create an instance of this class (only a single instance allowed).)
34 @item(Call @link(Initialize).)
35 @item(As necessary, use @link(SendEvent) to report custom events.)
36 )
37
38 Note that Tenjin is not part of TAnalytics, as you may want to send less events
39 to Tenjin than to other analytics services -- as Tenjin's main strength is in
40 the installation attribution, and custom events are paid above a certain count.
41 }
42 TCastleTenjin = class(TComponent)
43 strict private
44 FLastApiKey: string;
45 procedure ReinitializeJavaActivity(Sender: TObject);
46 public
47 constructor Create(AOwner: TComponent); override;
48 destructor Destroy; override;
49
50 { Initialize Tenjin analytics. }
51 procedure Initialize(const ApiKey: String);
52 { Send a custom event name to Tenjin. }
53 procedure SendEvent(const EventName: String);
54 end;
55
56 implementation
57
58 uses CastleApplicationProperties, CastleMessaging;
59
60 constructor TCastleTenjin.Create(AOwner: TComponent);
61 begin
62 inherited;
63 ApplicationProperties.OnInitializeJavaActivity.Add({$ifdef FPC}@{$endif} ReinitializeJavaActivity);
64 end;
65
66 destructor TCastleTenjin.Destroy;
67 begin
68 if ApplicationProperties(false) <> nil then
69 ApplicationProperties(false).OnInitializeJavaActivity.Remove({$ifdef FPC}@{$endif} ReinitializeJavaActivity);
70 inherited;
71 end;
72
73 procedure TCastleTenjin.ReinitializeJavaActivity(Sender: TObject);
74 begin
75 { in case Java activity got killed and is created again, reinitialize services }
76 if FLastApiKey <> '' then
77 Initialize(FLastApiKey);
78 end;
79
80 procedure TCastleTenjin.Initialize(const ApiKey: String);
81 begin
82 FLastApiKey := ApiKey;
83 Messaging.Send(['tenjin-initialize', ApiKey]);
84 end;
85
86 procedure TCastleTenjin.SendEvent(const EventName: String);
87 begin
88 Messaging.Send(['tenjin-send-event', EventName]);
89 end;
90
91 end.