-
Notifications
You must be signed in to change notification settings - Fork 133
Add feature to deferInitialization #214
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,12 @@ AmplitudeClient.prototype.init = function init(apiKey, opt_userId, opt_config, o | |
this.options.apiKey = apiKey; | ||
this._storageSuffix = '_' + apiKey + this._legacyStorageSuffix; | ||
|
||
var hasExistingCookie = !!this.cookieStorage.get(this.options.cookieName + this._storageSuffix); | ||
if (opt_config && opt_config.deferInitialization && !hasExistingCookie) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note for existing users, they won't get the option to defer tracking because they will already have a cookie, i guess we just need to communicate that to our customers and the people that requested this feature There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, this will only work on new users without existing cookies and they have to add the config flag |
||
this._deferInitialization(apiKey, opt_userId, opt_config, opt_callback); | ||
return; | ||
} | ||
|
||
_parseConfig(this.options, opt_config); | ||
|
||
if (type(this.options.logLevel) === 'string') { | ||
|
@@ -766,6 +772,10 @@ AmplitudeClient.prototype.saveEvents = function saveEvents() { | |
* @example amplitudeClient.setDomain('.amplitude.com'); | ||
*/ | ||
AmplitudeClient.prototype.setDomain = function setDomain(domain) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setDomain'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!utils.validateInput(domain, 'domain', 'string')) { | ||
return; | ||
} | ||
|
@@ -791,6 +801,10 @@ AmplitudeClient.prototype.setDomain = function setDomain(domain) { | |
* @example amplitudeClient.setUserId('joe@gmail.com'); | ||
*/ | ||
AmplitudeClient.prototype.setUserId = function setUserId(userId) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setUserId'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
try { | ||
this.options.userId = (userId !== undefined && userId !== null && ('' + userId)) || null; | ||
_saveCookieData(this); | ||
|
@@ -813,6 +827,10 @@ AmplitudeClient.prototype.setUserId = function setUserId(userId) { | |
* @example amplitudeClient.setGroup('orgId', 15); // this adds the current user to orgId 15. | ||
*/ | ||
AmplitudeClient.prototype.setGroup = function(groupType, groupName) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setGroup'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!this._apiKeySet('setGroup()') || !utils.validateInput(groupType, 'groupType', 'string') || | ||
utils.isEmptyString(groupType)) { | ||
return; | ||
|
@@ -831,6 +849,10 @@ AmplitudeClient.prototype.setGroup = function(groupType, groupName) { | |
* @example: amplitude.setOptOut(true); | ||
*/ | ||
AmplitudeClient.prototype.setOptOut = function setOptOut(enable) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setOptOut'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!utils.validateInput(enable, 'enable', 'boolean')) { | ||
return; | ||
} | ||
|
@@ -868,6 +890,10 @@ AmplitudeClient.prototype.resetSessionId = function resetSessionId() { | |
* @public | ||
*/ | ||
AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['regenerateDeviceId'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
this.setDeviceId(UUID() + 'R'); | ||
}; | ||
|
||
|
@@ -880,6 +906,10 @@ AmplitudeClient.prototype.regenerateDeviceId = function regenerateDeviceId() { | |
* @example amplitudeClient.setDeviceId('45f0954f-eb79-4463-ac8a-233a6f45a8f0'); | ||
*/ | ||
AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setDeviceId'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!utils.validateInput(deviceId, 'deviceId', 'string')) { | ||
return; | ||
} | ||
|
@@ -903,8 +933,8 @@ AmplitudeClient.prototype.setDeviceId = function setDeviceId(deviceId) { | |
* @example amplitudeClient.setUserProperties({'gender': 'female', 'sign_up_complete': true}) | ||
*/ | ||
AmplitudeClient.prototype.setUserProperties = function setUserProperties(userProperties) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['identify', userProperties]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setUserProperties'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
if (!this._apiKeySet('setUserProperties()') || !utils.validateInput(userProperties, 'userProperties', 'object')) { | ||
return; | ||
|
@@ -931,6 +961,10 @@ AmplitudeClient.prototype.setUserProperties = function setUserProperties(userPro | |
* @example amplitudeClient.clearUserProperties(); | ||
*/ | ||
AmplitudeClient.prototype.clearUserProperties = function clearUserProperties(){ | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['clearUserProperties'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!this._apiKeySet('clearUserProperties()')) { | ||
return; | ||
} | ||
|
@@ -967,8 +1001,8 @@ var _convertProxyObjectToRealObject = function _convertProxyObjectToRealObject(i | |
* amplitude.identify(identify); | ||
*/ | ||
AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['identify', identify_obj, opt_callback]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['identify'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
if (!this._apiKeySet('identify()')) { | ||
if (type(opt_callback) === 'function') { | ||
|
@@ -1002,8 +1036,8 @@ AmplitudeClient.prototype.identify = function(identify_obj, opt_callback) { | |
}; | ||
|
||
AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, identify_obj, opt_callback) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['groupIdentify', group_type, group_name, identify_obj, opt_callback]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['groupIdentify'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
if (!this._apiKeySet('groupIdentify()')) { | ||
if (type(opt_callback) === 'function') { | ||
|
@@ -1058,6 +1092,10 @@ AmplitudeClient.prototype.groupIdentify = function(group_type, group_name, ident | |
* @example amplitudeClient.setVersionName('1.12.3'); | ||
*/ | ||
AmplitudeClient.prototype.setVersionName = function setVersionName(versionName) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['setVersionName'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!utils.validateInput(versionName, 'versionName', 'string')) { | ||
return; | ||
} | ||
|
@@ -1217,8 +1255,8 @@ AmplitudeClient.prototype._limitEventsQueued = function _limitEventsQueued(queue | |
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15}); | ||
*/ | ||
AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventProperties, opt_callback) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['logEvent', eventType, eventProperties, opt_callback]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['logEvent'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
return this.logEventWithTimestamp(eventType, eventProperties, null, opt_callback); | ||
}; | ||
|
@@ -1234,8 +1272,8 @@ AmplitudeClient.prototype.logEvent = function logEvent(eventType, eventPropertie | |
* @example amplitudeClient.logEvent('Clicked Homepage Button', {'finished_flow': false, 'clicks': 15}); | ||
*/ | ||
AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, eventProperties, timestamp, opt_callback) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['logEventWithTimestamp', eventType, eventProperties, timestamp, opt_callback]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['logEventWithTimestamp'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
if (!this._apiKeySet('logEvent()')) { | ||
if (type(opt_callback) === 'function') { | ||
|
@@ -1274,8 +1312,8 @@ AmplitudeClient.prototype.logEventWithTimestamp = function logEvent(eventType, e | |
* @example amplitudeClient.logEventWithGroups('Clicked Button', null, {'orgId': 24}); | ||
*/ | ||
AmplitudeClient.prototype.logEventWithGroups = function(eventType, eventProperties, groups, opt_callback) { | ||
if (this._pendingReadStorage) { | ||
return this._q.push(['logEventWithGroups', eventType, eventProperties, groups, opt_callback]); | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['logEventWithGroups'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
if (!this._apiKeySet('logEventWithGroups()')) { | ||
if (type(opt_callback) === 'function') { | ||
|
@@ -1311,6 +1349,10 @@ var _isNumber = function _isNumber(n) { | |
* amplitude.logRevenueV2(revenue); | ||
*/ | ||
AmplitudeClient.prototype.logRevenueV2 = function logRevenueV2(revenue_obj) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['logRevenueV2'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
if (!this._apiKeySet('logRevenueV2()')) { | ||
return; | ||
} | ||
|
@@ -1341,6 +1383,10 @@ if (BUILD_COMPAT_2_0) { | |
* @example amplitudeClient.logRevenue(3.99, 1, 'product_1234'); | ||
*/ | ||
AmplitudeClient.prototype.logRevenue = function logRevenue(price, quantity, product) { | ||
if (this._shouldDeferCall()) { | ||
return this._q.push(['logRevenue'].concat(Array.prototype.slice.call(arguments, 0))); | ||
} | ||
|
||
// Test that the parameters are of the right type. | ||
if (!this._apiKeySet('logRevenue()') || !_isNumber(price) || (quantity !== undefined && !_isNumber(quantity))) { | ||
// utils.log('Price and quantity arguments to logRevenue must be numbers'); | ||
|
@@ -1552,4 +1598,35 @@ if (BUILD_COMPAT_2_0) { | |
*/ | ||
AmplitudeClient.prototype.__VERSION__ = version; | ||
|
||
/** | ||
* Determines whether or not to push call to this._q or invoke it | ||
* @private | ||
*/ | ||
AmplitudeClient.prototype._shouldDeferCall = function _shouldDeferCall() { | ||
return this._pendingReadStorage || this._initializationDeferred; | ||
djih marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
/** | ||
* Defers Initialization by putting all functions into storage until users | ||
* have accepted terms for tracking | ||
* @private | ||
*/ | ||
AmplitudeClient.prototype._deferInitialization = function _deferInitialization() { | ||
this._initializationDeferred = true; | ||
this._q.push(['init'].concat(Array.prototype.slice.call(arguments, 0))); | ||
}; | ||
|
||
/** | ||
* Enable tracking via logging events and dropping a cookie | ||
* Intended to be used with the deferInitialization configuration flag | ||
* This will drop a cookie and reset initialization deferred | ||
* @public | ||
*/ | ||
AmplitudeClient.prototype.enableTracking = function enableTracking() { | ||
// This will call init (which drops the cookie) and will run any pending tasks | ||
djih marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this._initializationDeferred = false; | ||
_saveCookieData(this); | ||
this.runQueuedFunctions(); | ||
djih marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export default AmplitudeClient; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved into the try catch to ensure that
this._storageSuffix
is definedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i kind of like that you're using the cookie to signal if the user has accepted tracking
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i feel like that's the safest bet to carry over. i haven't fully thought of the case where our cookie expires tho.... 😬