Skip to content

Commit 689e117

Browse files
committed
GROWTH-1094 add logAttributionCapturedEvent option to log Amplitude Events when attribution data is captured
1 parent 48e226e commit 689e117

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/amplitude-client.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,28 @@ AmplitudeClient.prototype._migrateUnsentEvents = function _migrateUnsentEvents(c
354354
* @private
355355
*/
356356
AmplitudeClient.prototype._trackParamsAndReferrer = function _trackParamsAndReferrer() {
357+
let utmProperties;
358+
let referrerProperties
359+
let gclidProperties;
357360
if (this.options.includeUtm) {
358-
this._initUtmData();
361+
utmProperties = this._initUtmData();
359362
}
360363
if (this.options.includeReferrer) {
361-
this._saveReferrer(this._getReferrer());
364+
referrerProperties = this._saveReferrer(this._getReferrer());
362365
}
363366
if (this.options.includeGclid) {
364-
this._saveGclid(this._getUrlParams());
367+
gclidProperties = this._saveGclid(this._getUrlParams());
368+
}
369+
if (this.options.logAttributionCapturedEvent) {
370+
const attributionProperties = Object.assign(
371+
{},
372+
utmProperties,
373+
referrerProperties,
374+
gclidProperties
375+
);
376+
if (Object.keys(attributionProperties).length > 0) {
377+
this.logEvent(Constants.ATTRIBUTION_EVENT, attributionProperties);
378+
}
365379
}
366380
};
367381

@@ -675,6 +689,7 @@ AmplitudeClient.prototype._initUtmData = function _initUtmData(queryParams, cook
675689
cookieParams = cookieParams || this.cookieStorage.get('__utmz');
676690
var utmProperties = getUtmData(cookieParams, queryParams);
677691
_sendParamsReferrerUserProperties(this, utmProperties);
692+
return utmProperties;
678693
};
679694

680695
/**
@@ -739,6 +754,7 @@ AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
739754
}
740755
var gclidProperties = {'gclid': gclid};
741756
_sendParamsReferrerUserProperties(this, gclidProperties);
757+
return gclidProperties;
742758
};
743759

744760
/**
@@ -778,6 +794,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
778794
'referring_domain': this._getReferringDomain(referrer)
779795
};
780796
_sendParamsReferrerUserProperties(this, referrerInfo);
797+
return referrerInfo;
781798
};
782799

783800
/**

src/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ export default {
3737
UTM_MEDIUM: 'utm_medium',
3838
UTM_CAMPAIGN: 'utm_campaign',
3939
UTM_TERM: 'utm_term',
40-
UTM_CONTENT: 'utm_content'
40+
UTM_CONTENT: 'utm_content',
41+
42+
ATTRIBUTION_EVENT: 'attribution captured'
4143
};

src/options.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default {
3030
includeUtm: false,
3131
language: language.getLanguage(),
3232
logLevel: 'WARN',
33+
logAttributionCapturedEvent: false,
3334
optOut: false,
3435
onError: () => {},
3536
platform,

test/browser/amplitudejs.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@
8989
}
9090
</script>
9191
<script>
92-
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true, includeUtm: true, includeGclid: true}, function() {
92+
amplitude.init('a2dbce0e18dfe5f8e74493843ff5c053', null, {includeReferrer: true, includeUtm: true, includeGclid: true, logAttributionCapturedEvent: true}, function() {
9393
alert(amplitude.options.deviceId);
9494
});
9595
amplitude.setVersionName('Web');

0 commit comments

Comments
 (0)