diff --git a/src/amplitude-client.js b/src/amplitude-client.js index 78722ddd..abe69e46 100644 --- a/src/amplitude-client.js +++ b/src/amplitude-client.js @@ -354,17 +354,45 @@ AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(c * @private */ AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndReferrer() { + const { utmProperties, referrerProperties, gclidProperties } = this._getParamsAndReferrer(); + if (this.options.includeUtm) { - this._initUtmData(); + this._initUtmData(utmProperties); } if (this.options.includeReferrer) { - this._saveReferrer(this._getReferrer()); + this._saveReferrer(referrerProperties); } if (this.options.includeGclid) { - this._saveGclid(this._getUrlParams()); + this._saveGclid(gclidProperties); } }; +/** + * Fetch all utm, referral, and gclid data + * @private + */ +AmplitudeClient.prototype._getParamsAndReferrer = function _getParamsAndReferrer() { + let utmProperties = null; + let referrerProperties = null; + let gclidProperties = null; + + if (this.options.includeUtm) { + utmProperties = this._getUtmProperties(); + } + if (this.options.includeReferrer) { + referrerProperties = this._getReferrerProperties(this._getReferrer()); + } + if (this.options.includeGclid) { + gclidProperties = this._getGclidProperties(this._getUrlParams()); + } + + return { + utmProperties, + referrerProperties, + gclidProperties, + }; +}; + /** * Parse and validate user specified config values and overwrite existing option value * DEFAULT_OPTIONS provides list of all config keys that are modifiable, as well as expected types for values @@ -667,13 +695,20 @@ var _saveCookieData = function _saveCookieData(scope) { }; /** - * Parse the utm properties out of cookies and query for adding to user properties. + * Parse the utm properties out of cookies and query params * @private */ -AmplitudeClient.prototype._initUtmData = function _initUtmData(queryParams, cookieParams) { +AmplitudeClient.prototype._getUtmProperties = function _getUtmProperties(queryParams, cookieParams) { queryParams = queryParams || this._getUrlParams(); cookieParams = cookieParams || this.cookieStorage.get('__utmz'); - var utmProperties = getUtmData(cookieParams, queryParams); + return getUtmData(cookieParams, queryParams); +}; + +/** + * Accepts parsed utm properties and adds to user properties + * @private + */ +AmplitudeClient.prototype._initUtmData = function _initUtmData(utmProperties) { _sendParamsReferrerUserProperties(this, utmProperties); }; @@ -729,15 +764,22 @@ AmplitudeClient.prototype._getUrlParams = function _getUrlParams() { }; /** - * Try to fetch Google Gclid from url params. + * Attempts to fetch Google Gclid from url params. * @private */ -AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) { +AmplitudeClient.prototype._getGclidProperties = function _getGclidProperties(urlParams) { var gclid = utils.getQueryParam('gclid', urlParams); if (utils.isEmptyString(gclid)) { return; } - var gclidProperties = {'gclid': gclid}; + return {'gclid': gclid}; +}; + +/** + * Adds Google Gclid as user properties + * @private + */ +AmplitudeClient.prototype._saveGclid = function _saveGclid(gclidProperties) { _sendParamsReferrerUserProperties(this, gclidProperties); }; @@ -765,19 +807,26 @@ AmplitudeClient.prototype._getReferringDomain = function _getReferringDomain(ref }; /** - * Fetch the referrer information, parse the domain and send. - * Since user properties are propagated on the server, only send once per session, don't need to send with every event + * Gets the referrer information and the domain. * @private */ -AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) { +AmplitudeClient.prototype._getReferrerProperties = function _getReferrerProperties(referrer) { if (utils.isEmptyString(referrer)) { return; } - var referrerInfo = { + return { 'referrer': referrer, 'referring_domain': this._getReferringDomain(referrer) }; - _sendParamsReferrerUserProperties(this, referrerInfo); +}; + +/** + * Accepts parsed referrer properties and adds to user properties + * Since user properties are propagated on the server, only send once per session, don't need to send with every event + * @private + */ +AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrerProperties) { + _sendParamsReferrerUserProperties(this, referrerProperties); }; /** diff --git a/test/amplitude-client.js b/test/amplitude-client.js index 53813cb9..1094da45 100644 --- a/test/amplitude-client.js +++ b/test/amplitude-client.js @@ -2626,8 +2626,9 @@ describe('setVersionName', function() { reset(); cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new'); var utmParams = '?utm_source=amplitude&utm_medium=email&utm_term=terms'; + var utmProperties = amplitude._getUtmProperties(utmParams); clock.tick(30 * 60 * 1000 + 1); - amplitude._initUtmData(utmParams); + amplitude._initUtmData(utmProperties); var expectedProperties = { utm_campaign: 'new', diff --git a/test/amplitude.js b/test/amplitude.js index ef353388..50a6cf25 100644 --- a/test/amplitude.js +++ b/test/amplitude.js @@ -1954,8 +1954,9 @@ describe('setVersionName', function() { reset(); cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new'); var utmParams = '?utm_source=amplitude&utm_medium=email&utm_term=terms'; + var utmProperties = amplitude.getInstance()._getUtmProperties(utmParams); clock.tick(30 * 60 * 1000 + 1); - amplitude.getInstance()._initUtmData(utmParams); + amplitude.getInstance()._initUtmData(utmProperties); var expectedProperties = { utm_campaign: 'new', diff --git a/test/browser/amplitudejs.html b/test/browser/amplitudejs.html index 4c60be84..94a7170e 100644 --- a/test/browser/amplitudejs.html +++ b/test/browser/amplitudejs.html @@ -27,7 +27,7 @@ 'setOptOut', 'setVersionName', 'setDomain', 'setDeviceId', 'setGlobalUserProperties', 'identify', 'clearUserProperties', 'setGroup', 'logRevenueV2', 'regenerateDeviceId', - 'logEventWithTimestamp', 'logEventWithGroups']; + 'logEventWithTimestamp', 'logEventWithGroups', 'onInit']; function setUpProxy(instance) { function proxyMain(fn) { instance[fn] = function() {