| 1 |
{ |
| 2 |
Copyright 2015-2022 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 |
{ Analytics (TAnalytics). } |
| 17 |
unit CastleAnalytics; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses Classes, CastleTimeUtils; |
| 24 |
|
| 25 |
type |
| 26 |
{ Status for @link(TAnalytics.Progress). } |
| 27 |
TAnalyticsProgress = ( |
| 28 |
apStart, |
| 29 |
apFail, |
| 30 |
apComplete |
| 31 |
); |
| 32 |
|
| 33 |
{ Gathering analytics through Google Analytics https://www.google.com/analytics/ |
| 34 |
and / or Game Analytics http://www.gameanalytics.com/ . |
| 35 |
|
| 36 |
Available on Android or iOS. You will need to also add appropriate "service" to your application, |
| 37 |
like @code(game_analytics) or @code(google_analytics) to have the integration code |
| 38 |
automatically included. See Android services |
| 39 |
( https://castle-engine.io/android_services ), |
| 40 |
iOS services |
| 41 |
( https://castle-engine.io/ios_services ) |
| 42 |
and CastleEngineManifest.xml docs |
| 43 |
( https://castle-engine.io/project_manifest ). |
| 44 |
|
| 45 |
Usage: |
| 46 |
|
| 47 |
@orderedList( |
| 48 |
@item(Create an instance of this class (only a single instance allowed).) |
| 49 |
@item(Initialize at least one analytics backend using the @link(InitializeGoogleAnalytics) |
| 50 |
or @link(InitializeGameAnalytics) method.) |
| 51 |
@item(Use the remaining methods, like @link(Event), to report events |
| 52 |
in your app.) |
| 53 |
) |
| 54 |
} |
| 55 |
TAnalytics = class(TComponent) |
| 56 |
private |
| 57 |
FLastGoogleAnalyticsPropertyId: String; |
| 58 |
FLastGameAnalyticsGameKey, FLastGameAnalyticsSecretKey: String; |
| 59 |
procedure ReinitializeJavaActivity(Sender: TObject); |
| 60 |
procedure CheckValidName(const S: String); |
| 61 |
public |
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
destructor Destroy; override; |
| 64 |
|
| 65 |
{ Initialize Google Analytics https://www.google.com/analytics/ . |
| 66 |
Usually called from @link(TCastleApplication.OnInitialize). } |
| 67 |
procedure InitializeGoogleAnalytics(const AnalyticsPropertyId: String); |
| 68 |
|
| 69 |
{ Initialize Game Analytics http://www.gameanalytics.com/ . |
| 70 |
Usually called from @link(TCastleApplication.OnInitialize). } |
| 71 |
procedure InitializeGameAnalytics(const GameKey, SecretKey: String); |
| 72 |
|
| 73 |
{ Send to analytics view of the screen, e.g. when user switches |
| 74 |
between UI states. |
| 75 |
|
| 76 |
@raises(EInvalidChar If the screen name contains invalid characters. |
| 77 |
Use only ASCII letters, digits, hyphens, underscores.) } |
| 78 |
procedure ScreenView(const ScreenName: String); |
| 79 |
|
| 80 |
{ Send to analytics a general event. |
| 81 |
|
| 82 |
@raises(EInvalidChar If some string contains invalid characters. |
| 83 |
Use only ASCII letters, digits, hyphens, underscores for the category |
| 84 |
and other strings.) } |
| 85 |
procedure Event(const Category, Action, ALabel: String; const Value: Int64); overload; |
| 86 |
|
| 87 |
{ Send to analytics a general event, along with a custom dimension. |
| 88 |
DimensionIndex must be > 0. |
| 89 |
|
| 90 |
See https://developers.google.com/analytics/devguides/collection/android/v4/customdimsmets#overview |
| 91 |
about what is a custom dimension for Google Analytics (you need to create |
| 92 |
the dimension index first in Google Analytics console). |
| 93 |
|
| 94 |
For Game Analytics, this is just used as an extra subcategory. |
| 95 |
Do not create too many different DimensionIndex + DimensionValue combinations, |
| 96 |
as each combination creates a new unique event id, |
| 97 |
and these are limited, see http://www.gameanalytics.com/docs/custom-events . |
| 98 |
|
| 99 |
@raises(EInvalidChar If some string contains invalid characters. |
| 100 |
Use only ASCII letters, digits, hyphens, underscores for the category |
| 101 |
and other strings.) } |
| 102 |
procedure Event(const Category, Action, ALabel: String; const Value: Int64; |
| 103 |
const DimensionIndex: Cardinal; const DimensionValue: String); overload; |
| 104 |
|
| 105 |
{ Send to analytics a timing event. |
| 106 |
|
| 107 |
@raises(EInvalidChar If some string contains invalid characters. |
| 108 |
Use only ASCII letters, digits, hyphens, underscores for the category |
| 109 |
and other strings.) } |
| 110 |
procedure Timing(const Category, AVariable, ALabel: String; const Time: TFloatTime); |
| 111 |
|
| 112 |
{ Send to analytics a progress event. |
| 113 |
|
| 114 |
@raises(EInvalidChar If some string contains invalid characters. |
| 115 |
Use only ASCII letters, digits, hyphens, underscores for the world |
| 116 |
and other strings.) } |
| 117 |
procedure Progress(const Status: TAnalyticsProgress; |
| 118 |
const World: String; |
| 119 |
const Level: String = ''; const Phase: String = ''; const Score: Integer = 0); |
| 120 |
end; |
| 121 |
|
| 122 |
implementation |
| 123 |
|
| 124 |
uses SysUtils, |
| 125 |
CastleMessaging, CastleApplicationProperties, CastleStringUtils; |
| 126 |
|
| 127 |
constructor TAnalytics.Create(AOwner: TComponent); |
| 128 |
begin |
| 129 |
inherited; |
| 130 |
ApplicationProperties.OnInitializeJavaActivity.Add({$ifdef FPC}@{$endif} ReinitializeJavaActivity); |
| 131 |
end; |
| 132 |
|
| 133 |
destructor TAnalytics.Destroy; |
| 134 |
begin |
| 135 |
if ApplicationProperties(false) <> nil then |
| 136 |
ApplicationProperties(false).OnInitializeJavaActivity.Remove({$ifdef FPC}@{$endif} ReinitializeJavaActivity); |
| 137 |
inherited; |
| 138 |
end; |
| 139 |
|
| 140 |
procedure TAnalytics.ReinitializeJavaActivity(Sender: TObject); |
| 141 |
begin |
| 142 |
{ in case Java activity got killed and is created again, reinitialize services } |
| 143 |
if FLastGoogleAnalyticsPropertyId <> '' then |
| 144 |
InitializeGoogleAnalytics(FLastGoogleAnalyticsPropertyId); |
| 145 |
if (FLastGameAnalyticsGameKey <> '') and |
| 146 |
(FLastGameAnalyticsSecretKey <> '') then |
| 147 |
InitializeGameAnalytics(FLastGameAnalyticsGameKey, FLastGameAnalyticsSecretKey); |
| 148 |
end; |
| 149 |
|
| 150 |
procedure TAnalytics.InitializeGoogleAnalytics(const AnalyticsPropertyId: String); |
| 151 |
begin |
| 152 |
FLastGoogleAnalyticsPropertyId := AnalyticsPropertyId; |
| 153 |
Messaging.Send(['google-analytics-initialize', AnalyticsPropertyId]); |
| 154 |
end; |
| 155 |
|
| 156 |
procedure TAnalytics.InitializeGameAnalytics(const GameKey, SecretKey: String); |
| 157 |
begin |
| 158 |
FLastGameAnalyticsGameKey := GameKey; |
| 159 |
FLastGameAnalyticsSecretKey := SecretKey; |
| 160 |
Messaging.Send(['game-analytics-initialize', GameKey, SecretKey]); |
| 161 |
end; |
| 162 |
|
| 163 |
procedure TAnalytics.ScreenView(const ScreenName: String); |
| 164 |
begin |
| 165 |
Messaging.Send(['analytics-send-screen-view', ScreenName]); |
| 166 |
end; |
| 167 |
|
| 168 |
procedure TAnalytics.Event(const Category, Action, ALabel: String; const Value: Int64); |
| 169 |
begin |
| 170 |
Event(Category, Action, ALabel, Value, 0, ''); |
| 171 |
end; |
| 172 |
|
| 173 |
procedure TAnalytics.CheckValidName(const S: String); |
| 174 |
begin |
| 175 |
// TODO: for now, merely warning |
| 176 |
SCheckChars(S, ['a'..'z', 'A'..'Z', '0'..'9', '-', '_'], false); |
| 177 |
// GameAnalytics allows also ., ()!? |
| 178 |
// not sure what GoogleAnalytics allows |
| 179 |
end; |
| 180 |
|
| 181 |
procedure TAnalytics.Event(const Category, Action, ALabel: String; const Value: Int64; |
| 182 |
const DimensionIndex: Cardinal; const DimensionValue: String); |
| 183 |
begin |
| 184 |
CheckValidName(Category); |
| 185 |
CheckValidName(Action); |
| 186 |
CheckValidName(ALabel); |
| 187 |
CheckValidName(DimensionValue); |
| 188 |
Messaging.Send(['analytics-send-event', Category, Action, ALabel, |
| 189 |
IntToStr(Value), IntToStr(DimensionIndex), DimensionValue]); |
| 190 |
end; |
| 191 |
|
| 192 |
procedure TAnalytics.Timing(const Category, AVariable, ALabel: String; const Time: TFloatTime); |
| 193 |
begin |
| 194 |
CheckValidName(Category); |
| 195 |
CheckValidName(AVariable); |
| 196 |
CheckValidName(ALabel); |
| 197 |
Messaging.Send(['analytics-send-timing', |
| 198 |
Category, AVariable, ALabel, TMessaging.TimeToStr(Time)]); |
| 199 |
end; |
| 200 |
|
| 201 |
procedure TAnalytics.Progress(const Status: TAnalyticsProgress; |
| 202 |
const World, Level, Phase: String; const Score: Integer); |
| 203 |
begin |
| 204 |
CheckValidName(World); |
| 205 |
if World = '' then |
| 206 |
raise EInvalidChar.Create('World cannot be empty for TAnalytics.Progress call'); |
| 207 |
CheckValidName(Level); |
| 208 |
CheckValidName(Phase); |
| 209 |
Messaging.Send(['analytics-send-progress', |
| 210 |
IntToStr(Ord(Status)), World, Level, Phase, IntToStr(Score)]); |
| 211 |
end; |
| 212 |
|
| 213 |
end. |