Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 63 additions & 14 deletions src/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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);
};

/**
Expand Down
3 changes: 2 additions & 1 deletion test/amplitude-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion test/amplitude.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion test/browser/amplitudejs.html
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down