Skip to content
Merged
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
28 changes: 27 additions & 1 deletion integrations/amplitude/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ Amplitude.prototype.initialize = function() {

window.amplitude.getInstance().init(this.options.apiKey, null, {
includeUtm: this.options.trackUtmProperties,
includeReferrer: this.options.trackReferrer,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tells Amplitude never to send the referrer information themselves

batchEvents: this.options.batchEvents,
eventUploadThreshold: this.options.eventUploadThreshold,
eventUploadPeriodMillis: this.options.eventUploadPeriodMillis,
Expand Down Expand Up @@ -126,6 +125,8 @@ Amplitude.prototype.loaded = function() {
Amplitude.prototype.page = function(page) {
this.setDeviceIdFromAnonymousId(page);

if (this.options.trackReferrer) this.sendReferrer();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this occurs right after setDeviceIdFromAnonymousId so that the correct DeviceId is used.


var category = page.category();
var name = page.fullName();
var opts = this.options;
Expand Down Expand Up @@ -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() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This aims to capture the exact same logic as the native Amplitude logic here

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.)
Expand Down
27 changes: 27 additions & 0 deletions integrations/amplitude/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,33 @@ 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() {
Expand Down