forked from hyperfiction/HypGA
-
Notifications
You must be signed in to change notification settings - Fork 8
/
GAnalytics.hx
293 lines (200 loc) · 8.35 KB
/
GAnalytics.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package;
/*
Copyright (c) 2013, Hyperfiction
All rights reserved.
Update to Google Analytics V3 API and latest OpenFL API by Emiliano Angelini - Emibap
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if cpp
import cpp.Lib;
#elseif neko
import neko.Lib;
#end
#if (android && openfl)
import openfl.utils.JNI;
#end
class GAnalytics {
// -------o public
/**
* Start the Ga tracking session
*
* @public
* @param sUA : GA UA code ( String )
* @param iPeriod ( Int )
* @return void
*/
static public function startSession( sUA : String , iPeriod : Int = 15 ) : Void {
#if (android && openfl)
if (ganalytics_startNewSession_jni == null) ganalytics_startNewSession_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "startSession", "(Ljava/lang/String;I)V");
ganalytics_startNewSession_jni(sUA, iPeriod);
#elseif ios
ganalytics_startNewSession(sUA, iPeriod);
#end
}
/**
* Stop the GA tracking session
*
* @public
* @return void
*/
static public function stopSession( ) : Void {
#if (android && openfl)
#elseif ios
ganalytics_stopSession();
#end
}
/**
* Set the userID of a user
* https://developers.google.com/analytics/devguides/collection/android/v4/user-id
* https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters#uid
*
* @public
* @param sUserId : User Id ( String )
* @return void
*/
static public function setUserId( sUserId : String ) : Void {
#if (android && openfl)
if (ganalytics_setUserId_jni == null) ganalytics_setUserId_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "setUserId", "(Ljava/lang/String;)V");
ganalytics_setUserId_jni(setUserId);
#elseif ios
ganalytics_setUserId(setUserId);
#end
}
/**
* Track a screen view
*
* @public
* @param sScreen : Code of the screen to be tracked ( String )
* @return void
*/
static public function trackScreen( sScreen : String ) : Void {
#if (android && openfl)
if (ganalytics_trackScreen_jni == null) ganalytics_trackScreen_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "trackScreen", "(Ljava/lang/String;)V");
ganalytics_trackScreen_jni(sScreen);
#elseif ios
ganalytics_sendScreenView(sScreen);
#end
}
/**
* Track a event
*
* @public
* @param sCat : Event category ( String )
* @param sCat : Action ( String )
* @param sLabel : Event label ( String )
* @param value : Event value ( Int )
* @return void
*/
static public function trackEvent( sCat : String , sAction : String , sLabel : String , value : Int = 1 ) : Void {
#if (android && openfl)
if (ganalytics_trackEvent_jni == null) ganalytics_trackEvent_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "trackEvent", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I)V");
ganalytics_trackEvent_jni(sCat, sAction, sLabel, value);
#elseif ios
ganalytics_sendEvent(sCat, sAction, sLabel, value);
#end
}
/**
*
*
* @public
* @param sCat : Event category ( String )
* @param iInterval : Timing interval ( Int )
* @param sName : Timing name ( String )
* @param sLabel : Label ( String )
* @return void
*/
static public function sendTiming( sCat : String , iInterval : Int , sName : String , sLabel : String ) : Void {
#if (android && openfl)
if (ganalytics_sendTiming_jni == null) ganalytics_sendTiming_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "sendTiming", "(Ljava/lang/String;ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
ganalytics_sendTiming_jni(sCat, iInterval, sName, sLabel);
#elseif ios
ganalytics_sendTiming(sCat, iInterval, sName, sLabel);
#end
}
/**
* Set a custom dimension value
*
* @public
* @param iIndex : Index of the dimension ( Int )
* @param sValue : Dimension value ( String )
* @return void
*/
static public function setCustom_dimension( iIndex : Int , sValue : String ) : Void {
#if (android && openfl)
#elseif ios
ganalytics_setCustom_dimension(iIndex, sValue);
#end
}
/**
* Set a custom metric value
*
* @public
* @param iIndex : Index of the metric ( Int )
* @param sValue : Metric value ( String )
* @return void
*/
static public function setCustom_metric( iIndex : Int , iValue : Int ) : Void {
#if (android && openfl)
#elseif ios
ganalytics_setCustom_metric(iIndex, iValue);
#end
}
/**
* Track a social event
*
* @public
* @param sSocial_network :Targetted social network ( String )
* @param sAction : Action ( String )
* @return void
*/
static public function trackSocial( sSocial_network : String , sAction : String , sTarget : String ) : Void {
#if (android && openfl)
if (ganalytics_trackSocial_jni == null) ganalytics_trackSocial_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "trackSocial", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
ganalytics_trackSocial_jni(sSocial_network, sAction, sTarget);
#elseif ios
ganalytics_sendSocial(sSocial_network, sAction, sTarget);
#end
}
/**
* Send uncaught exception
*
* @public
* @param description : A description of the exception (up to 100 characters)
* @param fatal : Indicates whether the exception was fatal. True indicates fatal.
* @return void
*/
static public function sendUncaughtException(description:String, fatal:Bool):Void {
#if (android && openfl)
if (ganalytics_sendUncaughtException_jni == null) ganalytics_sendUncaughtException_jni = JNI.createStaticMethod ("org.haxe.extension.GAnalytics", "sendUncaughtException", "(Ljava/lang/String;Z)V");
ganalytics_sendUncaughtException_jni(description, fatal);
#elseif ios
ganalytics_sendUncaughtException(description, fatal);
#end
}
// -------o protected
// -------o misc
#if ios
private static var ganalytics_startNewSession = Lib.load ("ganalytics", "ganalytics_startNewSession", 2);
private static var ganalytics_stopSession = Lib.load ("ganalytics", "ganalytics_stopSession", 0);
private static var ganalytics_sendScreenView = Lib.load ("ganalytics", "ganalytics_sendScreenView", 1);
private static var ganalytics_sendEvent = Lib.load ("ganalytics", "ganalytics_sendEvent", 4);
private static var ganalytics_sendTiming = Lib.load ("ganalytics", "ganalytics_sendTiming", 4);
private static var ganalytics_setCustom_dimension = Lib.load ("ganalytics", "ganalytics_setCustom_dimension", 2);
private static var ganalytics_setCustom_metric = Lib.load ("ganalytics", "ganalytics_setCustom_metric", 2);
private static var ganalytics_sendSocial = Lib.load ("ganalytics", "ganalytics_sendSocial", 3);
private static var ganalytics_setUserId = Lib.load ("ganalytics", "ganalytics_setUserId", 1);
private static var ganalytics_sendUncaughtException = Lib.load ("ganalytics", "ganalytics_sendUncaughtException", 2);
#end
#if (android && openfl)
private static var ganalytics_startNewSession_jni: Dynamic;
private static var ganalytics_trackScreen_jni: Dynamic;
private static var ganalytics_trackEvent_jni: Dynamic;
private static var ganalytics_sendTiming_jni: Dynamic;
private static var ganalytics_trackSocial_jni: Dynamic;
private static var ganalytics_setUserId_jni: Dynamic;
private static var ganalytics_sendUncaughtException_jni: Dynamic;
#end
}