@@ -79,6 +79,12 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o
79
79
this . options . apiKey = apiKey ;
80
80
this . _storageSuffix = '_' + apiKey + this . _legacyStorageSuffix ;
81
81
82
+ var hasExistingCookie = ! ! this . cookieStorage . get ( this . options . cookieName + this . _storageSuffix ) ;
83
+ if ( opt_config && opt_config . deferInitialization && ! hasExistingCookie ) {
84
+ this . _deferInitialization ( apiKey , opt_userId , opt_config , opt_callback ) ;
85
+ return ;
86
+ }
87
+
82
88
_parseConfig ( this . options , opt_config ) ;
83
89
84
90
if ( type ( this . options . logLevel ) === 'string' ) {
@@ -766,6 +772,10 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() {
766
772
* @example amplitudeClient.setDomain('.amplitude.com');
767
773
*/
768
774
AmplitudeClient . prototype . setDomain = function setDomain ( domain ) {
775
+ if ( this . _shouldDeferCall ( ) ) {
776
+ return this . _q . push ( [ 'setDomain' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
777
+ }
778
+
769
779
if ( ! utils . validateInput ( domain , 'domain' , 'string' ) ) {
770
780
return ;
771
781
}
@@ -791,6 +801,10 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) {
791
801
* @example amplitudeClient.setUserId('[email protected] ');
792
802
*/
793
803
AmplitudeClient . prototype . setUserId = function setUserId ( userId ) {
804
+ if ( this . _shouldDeferCall ( ) ) {
805
+ return this . _q . push ( [ 'setUserId' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
806
+ }
807
+
794
808
try {
795
809
this . options . userId = ( userId !== undefined && userId !== null && ( '' + userId ) ) || null ;
796
810
_saveCookieData ( this ) ;
@@ -813,6 +827,10 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId) {
813
827
* @example amplitudeClient.setGroup('orgId', 15); // this adds the current user to orgId 15.
814
828
*/
815
829
AmplitudeClient . prototype . setGroup = function ( groupType , groupName ) {
830
+ if ( this . _shouldDeferCall ( ) ) {
831
+ return this . _q . push ( [ 'setGroup' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
832
+ }
833
+
816
834
if ( ! this . _apiKeySet ( 'setGroup()' ) || ! utils . validateInput ( groupType , 'groupType' , 'string' ) ||
817
835
utils . isEmptyString ( groupType ) ) {
818
836
return ;
@@ -831,6 +849,10 @@ AmplitudeClient.prototype.setGroup = function(groupType, groupName) {
831
849
* @example : amplitude.setOptOut(true);
832
850
*/
833
851
AmplitudeClient . prototype . setOptOut = function setOptOut ( enable ) {
852
+ if ( this . _shouldDeferCall ( ) ) {
853
+ return this . _q . push ( [ 'setOptOut' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
854
+ }
855
+
834
856
if ( ! utils . validateInput ( enable , 'enable' , 'boolean' ) ) {
835
857
return ;
836
858
}
@@ -868,6 +890,10 @@ AmplitudeClient.prototype.resetSessionId = function resetSessionId() {
868
890
* @public
869
891
*/
870
892
AmplitudeClient . prototype . regenerateDeviceId = function regenerateDeviceId ( ) {
893
+ if ( this . _shouldDeferCall ( ) ) {
894
+ return this . _q . push ( [ 'regenerateDeviceId' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
895
+ }
896
+
871
897
this . setDeviceId ( UUID ( ) + 'R' ) ;
872
898
} ;
873
899
@@ -880,6 +906,10 @@ AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() {
880
906
* @example amplitudeClient.setDeviceId('45f0954f-eb79-4463-ac8a-233a6f45a8f0');
881
907
*/
882
908
AmplitudeClient . prototype . setDeviceId = function setDeviceId ( deviceId ) {
909
+ if ( this . _shouldDeferCall ( ) ) {
910
+ return this . _q . push ( [ 'setDeviceId' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
911
+ }
912
+
883
913
if ( ! utils . validateInput ( deviceId , 'deviceId' , 'string' ) ) {
884
914
return ;
885
915
}
@@ -903,8 +933,8 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) {
903
933
* @example amplitudeClient.setUserProperties({'gender': 'female', 'sign_up_complete': true})
904
934
*/
905
935
AmplitudeClient . prototype . setUserProperties = function setUserProperties ( userProperties ) {
906
- if ( this . _pendingReadStorage ) {
907
- return this . _q . push ( [ 'identify' , userProperties ] ) ;
936
+ if ( this . _shouldDeferCall ( ) ) {
937
+ return this . _q . push ( [ 'setUserProperties' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
908
938
}
909
939
if ( ! this . _apiKeySet ( 'setUserProperties()' ) || ! utils . validateInput ( userProperties , 'userProperties' , 'object' ) ) {
910
940
return ;
@@ -931,6 +961,10 @@ AmplitudeClient.prototype.setUserProperties = function setUserProperties(userPro
931
961
* @example amplitudeClient.clearUserProperties();
932
962
*/
933
963
AmplitudeClient . prototype . clearUserProperties = function clearUserProperties ( ) {
964
+ if ( this . _shouldDeferCall ( ) ) {
965
+ return this . _q . push ( [ 'clearUserProperties' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
966
+ }
967
+
934
968
if ( ! this . _apiKeySet ( 'clearUserProperties()' ) ) {
935
969
return ;
936
970
}
@@ -967,8 +1001,8 @@ var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(i
967
1001
* amplitude.identify(identify);
968
1002
*/
969
1003
AmplitudeClient . prototype . identify = function ( identify_obj , opt_callback ) {
970
- if ( this . _pendingReadStorage ) {
971
- return this . _q . push ( [ 'identify' , identify_obj , opt_callback ] ) ;
1004
+ if ( this . _shouldDeferCall ( ) ) {
1005
+ return this . _q . push ( [ 'identify' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
972
1006
}
973
1007
if ( ! this . _apiKeySet ( 'identify()' ) ) {
974
1008
if ( type ( opt_callback ) === 'function' ) {
@@ -1002,8 +1036,8 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) {
1002
1036
} ;
1003
1037
1004
1038
AmplitudeClient . prototype . groupIdentify = function ( group_type , group_name , identify_obj , opt_callback ) {
1005
- if ( this . _pendingReadStorage ) {
1006
- return this . _q . push ( [ 'groupIdentify' , group_type , group_name , identify_obj , opt_callback ] ) ;
1039
+ if ( this . _shouldDeferCall ( ) ) {
1040
+ return this . _q . push ( [ 'groupIdentify' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1007
1041
}
1008
1042
if ( ! this . _apiKeySet ( 'groupIdentify()' ) ) {
1009
1043
if ( type ( opt_callback ) === 'function' ) {
@@ -1058,6 +1092,10 @@ AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, ident
1058
1092
* @example amplitudeClient.setVersionName('1.12.3');
1059
1093
*/
1060
1094
AmplitudeClient . prototype . setVersionName = function setVersionName ( versionName ) {
1095
+ if ( this . _shouldDeferCall ( ) ) {
1096
+ return this . _q . push ( [ 'setVersionName' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1097
+ }
1098
+
1061
1099
if ( ! utils . validateInput ( versionName , 'versionName' , 'string' ) ) {
1062
1100
return ;
1063
1101
}
@@ -1217,8 +1255,8 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue
1217
1255
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
1218
1256
*/
1219
1257
AmplitudeClient . prototype . logEvent = function logEvent ( eventType , eventProperties , opt_callback ) {
1220
- if ( this . _pendingReadStorage ) {
1221
- return this . _q . push ( [ 'logEvent' , eventType , eventProperties , opt_callback ] ) ;
1258
+ if ( this . _shouldDeferCall ( ) ) {
1259
+ return this . _q . push ( [ 'logEvent' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1222
1260
}
1223
1261
return this . logEventWithTimestamp ( eventType , eventProperties , null , opt_callback ) ;
1224
1262
} ;
@@ -1234,8 +1272,8 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie
1234
1272
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15});
1235
1273
*/
1236
1274
AmplitudeClient . prototype . logEventWithTimestamp = function logEvent ( eventType , eventProperties , timestamp , opt_callback ) {
1237
- if ( this . _pendingReadStorage ) {
1238
- return this . _q . push ( [ 'logEventWithTimestamp' , eventType , eventProperties , timestamp , opt_callback ] ) ;
1275
+ if ( this . _shouldDeferCall ( ) ) {
1276
+ return this . _q . push ( [ 'logEventWithTimestamp' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1239
1277
}
1240
1278
if ( ! this . _apiKeySet ( 'logEvent()' ) ) {
1241
1279
if ( type ( opt_callback ) === 'function' ) {
@@ -1274,8 +1312,8 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e
1274
1312
* @example amplitudeClient.logEventWithGroups('Clicked Button', null, {'orgId': 24});
1275
1313
*/
1276
1314
AmplitudeClient . prototype . logEventWithGroups = function ( eventType , eventProperties , groups , opt_callback ) {
1277
- if ( this . _pendingReadStorage ) {
1278
- return this . _q . push ( [ 'logEventWithGroups' , eventType , eventProperties , groups , opt_callback ] ) ;
1315
+ if ( this . _shouldDeferCall ( ) ) {
1316
+ return this . _q . push ( [ 'logEventWithGroups' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1279
1317
}
1280
1318
if ( ! this . _apiKeySet ( 'logEventWithGroups()' ) ) {
1281
1319
if ( type ( opt_callback ) === 'function' ) {
@@ -1311,6 +1349,10 @@ var _isNumber = function _isNumber(n) {
1311
1349
* amplitude.logRevenueV2(revenue);
1312
1350
*/
1313
1351
AmplitudeClient . prototype . logRevenueV2 = function logRevenueV2 ( revenue_obj ) {
1352
+ if ( this . _shouldDeferCall ( ) ) {
1353
+ return this . _q . push ( [ 'logRevenueV2' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1354
+ }
1355
+
1314
1356
if ( ! this . _apiKeySet ( 'logRevenueV2()' ) ) {
1315
1357
return ;
1316
1358
}
@@ -1341,6 +1383,10 @@ if (BUILD_COMPAT_2_0) {
1341
1383
* @example amplitudeClient.logRevenue(3.99, 1, 'product_1234');
1342
1384
*/
1343
1385
AmplitudeClient . prototype . logRevenue = function logRevenue ( price , quantity , product ) {
1386
+ if ( this . _shouldDeferCall ( ) ) {
1387
+ return this . _q . push ( [ 'logRevenue' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1388
+ }
1389
+
1344
1390
// Test that the parameters are of the right type.
1345
1391
if ( ! this . _apiKeySet ( 'logRevenue()' ) || ! _isNumber ( price ) || ( quantity !== undefined && ! _isNumber ( quantity ) ) ) {
1346
1392
// utils.log('Price and quantity arguments to logRevenue must be numbers');
@@ -1552,4 +1598,35 @@ if (BUILD_COMPAT_2_0) {
1552
1598
*/
1553
1599
AmplitudeClient . prototype . __VERSION__ = version ;
1554
1600
1601
+ /**
1602
+ * Determines whether or not to push call to this._q or invoke it
1603
+ * @private
1604
+ */
1605
+ AmplitudeClient . prototype . _shouldDeferCall = function _shouldDeferCall ( ) {
1606
+ return this . _pendingReadStorage || this . _initializationDeferred ;
1607
+ } ;
1608
+
1609
+ /**
1610
+ * Defers Initialization by putting all functions into storage until users
1611
+ * have accepted terms for tracking
1612
+ * @private
1613
+ */
1614
+ AmplitudeClient . prototype . _deferInitialization = function _deferInitialization ( ) {
1615
+ this . _initializationDeferred = true ;
1616
+ this . _q . push ( [ 'init' ] . concat ( Array . prototype . slice . call ( arguments , 0 ) ) ) ;
1617
+ } ;
1618
+
1619
+ /**
1620
+ * Enable tracking via logging events and dropping a cookie
1621
+ * Intended to be used with the deferInitialization configuration flag
1622
+ * This will drop a cookie and reset initialization deferred
1623
+ * @public
1624
+ */
1625
+ AmplitudeClient . prototype . enableTracking = function enableTracking ( ) {
1626
+ // This will call init (which drops the cookie) and will run any pending tasks
1627
+ this . _initializationDeferred = false ;
1628
+ _saveCookieData ( this ) ;
1629
+ this . runQueuedFunctions ( ) ;
1630
+ } ;
1631
+
1555
1632
export default AmplitudeClient ;
0 commit comments