Skip to content

Commit 309dac3

Browse files
authoredSep 19, 2020
feat: add logAttributionCapturedEvent option (#295)
* GROWTH-1094 add logAttributionCapturedEvent option to log Amplitude Events when attribution data is captured * Add spread property * Revert changes to test html * Rename ATTRIBUTION_EVENT to * GROWTH-1094 add missing semicolon * Rename ATTRIBUTION_CAPTURED * Add tests for feature
1 parent 61b6590 commit 309dac3

File tree

4 files changed

+126
-4
lines changed

4 files changed

+126
-4
lines changed
 

‎src/amplitude-client.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,27 @@ 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 = {
371+
...utmProperties,
372+
...referrerProperties,
373+
...gclidProperties,
374+
};
375+
if (Object.keys(attributionProperties).length > 0) {
376+
this.logEvent(Constants.ATTRIBUTION_EVENT, attributionProperties);
377+
}
365378
}
366379
};
367380

@@ -675,6 +688,7 @@ AmplitudeClient.prototype._initUtmData = function _initUtmData(queryParams, cook
675688
cookieParams = cookieParams || this.cookieStorage.get('__utmz');
676689
var utmProperties = getUtmData(cookieParams, queryParams);
677690
_sendParamsReferrerUserProperties(this, utmProperties);
691+
return utmProperties;
678692
};
679693

680694
/**
@@ -739,6 +753,7 @@ AmplitudeClient.prototype._saveGclid = function _saveGclid(urlParams) {
739753
}
740754
var gclidProperties = {'gclid': gclid};
741755
_sendParamsReferrerUserProperties(this, gclidProperties);
756+
return gclidProperties;
742757
};
743758

744759
/**
@@ -778,6 +793,7 @@ AmplitudeClient.prototype._saveReferrer = function _saveReferrer(referrer) {
778793
'referring_domain': this._getReferringDomain(referrer)
779794
};
780795
_sendParamsReferrerUserProperties(this, referrerInfo);
796+
return referrerInfo;
781797
};
782798

783799
/**

‎src/constants.js

+3-1
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: '[Amplitude] Attribution Captured'
4143
};

‎src/options.js

+1
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/amplitude-client.js

+103
Original file line numberDiff line numberDiff line change
@@ -2952,6 +2952,32 @@ describe('setVersionName', function() {
29522952
});
29532953
});
29542954

2955+
it('should log attribution event when referrer is updated if configured', function() {
2956+
clock.tick(30 * 60 * 1000 + 1); // force new session
2957+
amplitude.init(apiKey, undefined, {includeReferrer: true, logAttributionCapturedEvent: true, batchEvents: true, eventUploadThreshold: 2});
2958+
assert.lengthOf(server.requests, 1);
2959+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
2960+
assert.lengthOf(events, 2);
2961+
// first event should be identify with initial_referrer and referrer
2962+
assert.equal(events[0].event_type, '$identify');
2963+
assert.deepEqual(events[0].user_properties, {
2964+
'$set': {
2965+
'referrer': 'https://amplitude.com/contact',
2966+
'referring_domain': 'amplitude.com'
2967+
},
2968+
'$setOnce': {
2969+
'initial_referrer': 'https://amplitude.com/contact',
2970+
'initial_referring_domain': 'amplitude.com'
2971+
}
2972+
});
2973+
// second event should be the attribution captured event with referrer information
2974+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
2975+
assert.deepEqual(events[1].event_properties, {
2976+
'referrer': 'https://amplitude.com/contact',
2977+
'referring_domain': 'amplitude.com'
2978+
});
2979+
});
2980+
29552981
it('should not delete unsent events saved to localStorage', function() {
29562982
var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
29572983
'"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
@@ -2993,6 +3019,62 @@ describe('setVersionName', function() {
29933019
}
29943020
});
29953021
});
3022+
3023+
it('should log attribution event when UTMs are captured if configured', function() {
3024+
reset();
3025+
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
3026+
clock.tick(30 * 60 * 1000 + 1);
3027+
amplitude.init(apiKey, undefined, {includeUtm: true, logAttributionCapturedEvent: true, batchEvents: true, eventUploadThreshold: 2});
3028+
assert.lengthOf(server.requests, 1);
3029+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3030+
// first event should be identify with UTM state
3031+
assert.equal(events[0].event_type, '$identify');
3032+
assert.deepEqual(events[0].user_properties, {
3033+
'$setOnce': {
3034+
initial_utm_campaign: 'new',
3035+
initial_utm_content: 'top'
3036+
},
3037+
'$set': {
3038+
utm_campaign: 'new',
3039+
utm_content: 'top'
3040+
}
3041+
});
3042+
// second event should be the attribution captured event with UTMs populated
3043+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3044+
assert.deepEqual(events[1].event_properties, {
3045+
utm_campaign: 'new',
3046+
utm_content: 'top'
3047+
});
3048+
});
3049+
3050+
it('should log attribution event more than once per session if configured and UTMs changes', function() {
3051+
reset();
3052+
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
3053+
amplitude.init(apiKey, undefined, {
3054+
includeUtm: true, logAttributionCapturedEvent: true, saveParamsReferrerOncePerSession: false, batchEvents: true, eventUploadThreshold: 2
3055+
});
3056+
// even though same session, utm params are sent again
3057+
assert.lengthOf(server.requests, 1);
3058+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3059+
// first event should be identify with UTM state
3060+
assert.equal(events[0].event_type, '$identify');
3061+
assert.deepEqual(events[0].user_properties, {
3062+
'$setOnce': {
3063+
initial_utm_campaign: 'new',
3064+
initial_utm_content: 'top'
3065+
},
3066+
'$set': {
3067+
utm_campaign: 'new',
3068+
utm_content: 'top'
3069+
}
3070+
});
3071+
// second event should be the attribution captured event with UTMs populated
3072+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3073+
assert.deepEqual(events[1].event_properties, {
3074+
utm_campaign: 'new',
3075+
utm_content: 'top'
3076+
});
3077+
});
29963078
});
29973079

29983080
describe('gatherGclid', function() {
@@ -3050,6 +3132,27 @@ describe('setVersionName', function() {
30503132
'$setOnce': {'initial_gclid': '12345'}
30513133
});
30523134
});
3135+
3136+
it('should log attribution event when gclid is captured if configured', () => {
3137+
clock.tick(30 * 60 * 1000 + 1);
3138+
amplitude.init(apiKey, undefined, {includeGclid: true, logAttributionCapturedEvent: true, batchEvents: true, eventUploadThreshold: 2});
3139+
3140+
assert.lengthOf(server.requests, 1);
3141+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3142+
assert.lengthOf(events, 2);
3143+
3144+
// first event should be identify with gclid
3145+
assert.equal(events[0].event_type, '$identify');
3146+
assert.deepEqual(events[0].user_properties, {
3147+
'$set': {'gclid': '12345'},
3148+
'$setOnce': {'initial_gclid': '12345'}
3149+
});
3150+
// second event should be the attribution captured event with gclid populated
3151+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3152+
assert.deepEqual(events[1].event_properties, {
3153+
'gclid': '12345'
3154+
})
3155+
});
30533156
});
30543157

30553158
describe('logRevenue', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.