Skip to content

Commit fa40657

Browse files
committed
Add tests for feature
1 parent a6a221e commit fa40657

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

test/amplitude-client.js

+101
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,60 @@ 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+
assert.equal(events[0].event_type, '$identify');
3031+
assert.deepEqual(events[0].user_properties, {
3032+
'$setOnce': {
3033+
initial_utm_campaign: 'new',
3034+
initial_utm_content: 'top'
3035+
},
3036+
'$set': {
3037+
utm_campaign: 'new',
3038+
utm_content: 'top'
3039+
}
3040+
});
3041+
// second event should be the attribution captured event with UTMs populated
3042+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3043+
assert.deepEqual(events[1].event_properties, {
3044+
utm_campaign: 'new',
3045+
utm_content: 'top'
3046+
});
3047+
});
3048+
3049+
it('should log attribution event more than once per session if configured and UTMs changes', function() {
3050+
reset();
3051+
cookie.set('__utmz', '133232535.1424926227.1.1.utmcct=top&utmccn=new');
3052+
amplitude.init(apiKey, undefined, {
3053+
includeUtm: true, logAttributionCapturedEvent: true, saveParamsReferrerOncePerSession: false, batchEvents: true, eventUploadThreshold: 2
3054+
});
3055+
// even though same session, utm params are sent again
3056+
assert.lengthOf(server.requests, 1);
3057+
var events = JSON.parse(queryString.parse(server.requests[0].requestBody).e);
3058+
assert.equal(events[0].event_type, '$identify');
3059+
assert.deepEqual(events[0].user_properties, {
3060+
'$setOnce': {
3061+
initial_utm_campaign: 'new',
3062+
initial_utm_content: 'top'
3063+
},
3064+
'$set': {
3065+
utm_campaign: 'new',
3066+
utm_content: 'top'
3067+
}
3068+
});
3069+
// second event should be the attribution captured event with UTMs populated
3070+
assert.equal(events[1].event_type, constants.ATTRIBUTION_EVENT);
3071+
assert.deepEqual(events[1].event_properties, {
3072+
utm_campaign: 'new',
3073+
utm_content: 'top'
3074+
});
3075+
});
29963076
});
29973077

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

30553156
describe('logRevenue', function() {

0 commit comments

Comments
 (0)