From 45708e63ffb08a561f95b5994bbe1babbbfa27e3 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 4 Oct 2018 18:28:07 -0700 Subject: [PATCH 1/2] send referrer info --- integrations/amplitude/lib/index.js | 28 ++++++++++++++++++++++- integrations/amplitude/test/index.test.js | 24 +++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/integrations/amplitude/lib/index.js b/integrations/amplitude/lib/index.js index 6720b27ce..572dd107d 100644 --- a/integrations/amplitude/lib/index.js +++ b/integrations/amplitude/lib/index.js @@ -69,7 +69,6 @@ Amplitude.prototype.initialize = function() { window.amplitude.getInstance().init(this.options.apiKey, null, { includeUtm: this.options.trackUtmProperties, - includeReferrer: this.options.trackReferrer, batchEvents: this.options.batchEvents, eventUploadThreshold: this.options.eventUploadThreshold, eventUploadPeriodMillis: this.options.eventUploadPeriodMillis, @@ -126,6 +125,8 @@ Amplitude.prototype.loaded = function() { Amplitude.prototype.page = function(page) { this.setDeviceIdFromAnonymousId(page); + if (this.options.trackReferrer) this.sendReferrer(); + var category = page.category(); var name = page.fullName(); var opts = this.options; @@ -392,6 +393,31 @@ Amplitude.prototype.setRevenue = function(properties) { } }; +// equivalent of sending includeReferrer=true in the amplitude config object when we initialize +Amplitude.prototype.sendReferrer = function() { + var identify = new window.amplitude.Identify(); + + var referrer = this.getReferrer(); + if (!referrer || referrer.length === 0) return; + + identify.setOnce('initial_referrer', referrer); + identify.set('referrer', referrer); + + var parts = referrer.split('/'); + if (parts.length >= 3) { + var referring_domain = parts[2]; + identify.setOnce('initial_referring_domain', referring_domain); + identify.set('referring_domain', referring_domain); + } + + window.amplitude.getInstance().identify(identify); +} + +// wrapper for testing purposes +Amplitude.prototype.getReferrer = function() { + return document.referrer; +} + function mapRevenueAttributes(track) { // Revenue type can be anything such as Refund, Tax, etc. // Using mapper here to support future ecomm event => revenue mappings (Order Refund, etc.) diff --git a/integrations/amplitude/test/index.test.js b/integrations/amplitude/test/index.test.js index 49dca509c..8a357ab85 100644 --- a/integrations/amplitude/test/index.test.js +++ b/integrations/amplitude/test/index.test.js @@ -254,6 +254,30 @@ describe('Amplitude', function() { analytics.identify('id'); analytics.called(window.amplitude.getInstance().setDeviceId, 'example'); }); + + it('should send referrer if "trackReferrer" is set', function() { + var spy = sinon.spy(window.amplitude.getInstance(), 'identify'); + + var stub = sinon.stub(amplitude, 'getReferrer') + stub.returns('http://examplepage.com/') + + amplitude.options.trackReferrer = true; + + analytics.page(); + + sinon.assert.calledWith(spy, sinon.match({ + userPropertiesOperations: sinon.match({ + $setOnce: sinon.match({ + initial_referrer: 'http://examplepage.com/', + initial_referring_domain: 'examplepage.com' + }), + $set: sinon.match({ + referrer: 'http://examplepage.com/', + referring_domain: 'examplepage.com' + }) + }) + })); + }) }); describe('#identify', function() { From 569c43c81c2546a511ed065570297271bae33411 Mon Sep 17 00:00:00 2001 From: Justin Lee Date: Thu, 4 Oct 2018 18:29:40 -0700 Subject: [PATCH 2/2] formatting --- integrations/amplitude/lib/index.js | 4 ++-- integrations/amplitude/test/index.test.js | 29 +++++++++++++---------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/integrations/amplitude/lib/index.js b/integrations/amplitude/lib/index.js index 572dd107d..58731ae42 100644 --- a/integrations/amplitude/lib/index.js +++ b/integrations/amplitude/lib/index.js @@ -411,12 +411,12 @@ Amplitude.prototype.sendReferrer = function() { } window.amplitude.getInstance().identify(identify); -} +}; // wrapper for testing purposes Amplitude.prototype.getReferrer = function() { return document.referrer; -} +}; function mapRevenueAttributes(track) { // Revenue type can be anything such as Refund, Tax, etc. diff --git a/integrations/amplitude/test/index.test.js b/integrations/amplitude/test/index.test.js index 8a357ab85..5153b9e27 100644 --- a/integrations/amplitude/test/index.test.js +++ b/integrations/amplitude/test/index.test.js @@ -258,26 +258,29 @@ describe('Amplitude', function() { it('should send referrer if "trackReferrer" is set', function() { var spy = sinon.spy(window.amplitude.getInstance(), 'identify'); - var stub = sinon.stub(amplitude, 'getReferrer') - stub.returns('http://examplepage.com/') + var stub = sinon.stub(amplitude, 'getReferrer'); + stub.returns('http://examplepage.com/'); amplitude.options.trackReferrer = true; analytics.page(); - sinon.assert.calledWith(spy, sinon.match({ - userPropertiesOperations: sinon.match({ - $setOnce: sinon.match({ - initial_referrer: 'http://examplepage.com/', - initial_referring_domain: 'examplepage.com' - }), - $set: sinon.match({ - referrer: 'http://examplepage.com/', - referring_domain: 'examplepage.com' + sinon.assert.calledWith( + spy, + sinon.match({ + userPropertiesOperations: sinon.match({ + $setOnce: sinon.match({ + initial_referrer: 'http://examplepage.com/', + initial_referring_domain: 'examplepage.com' + }), + $set: sinon.match({ + referrer: 'http://examplepage.com/', + referring_domain: 'examplepage.com' + }) }) }) - })); - }) + ); + }); }); describe('#identify', function() {