Skip to content

Commit 9e58dd0

Browse files
committed
Add includeReferrer flag
1 parent f88a6f8 commit 9e58dd0

File tree

6 files changed

+112
-5
lines changed

6 files changed

+112
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Unreleased
22

3+
* Fix bug where multi-byte unicode characters were hashed improperly.
4+
* Add option to sent referrer information as user properties
5+
36
## 2.2.0 (May 5, 2015)
47

58
* Use gzipped version of the library by default. If you still need the uncompressed version, remove ".gz" from the script url in your integration snippet.

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ You can configure Amplitude by passing an object as the third argument to the `i
8686
amplitude.init("YOUR_API_KEY_HERE", null, {
8787
// optional configuration options
8888
saveEvents: true,
89-
includeUtm: true
89+
includeUtm: true,
90+
includeReferrer: true
9091
})
9192

9293
| option | description | default |
@@ -95,6 +96,7 @@ You can configure Amplitude by passing an object as the third argument to the `i
9596
| savedMaxCount | Maximum number of events to save in localStorage. If more events are logged while offline, old events are removed. | 1000 |
9697
| uploadBatchSize | Maximum number of events to send to the server per request. | 100 |
9798
| includeUtm | If `true`, finds utm parameters in the query string or the __utmz cookie, parses, and includes them as user propeties on all events uploaded. | `false` |
99+
| includeReferrer | If `true`, includes `referrer` and `referring_domain` as user propeties on all events uploaded. | `false` |
98100

99101

100102
# Advanced #

amplitude.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Amplitude.prototype._newSession = false;
170170
* opt_config Configuration options
171171
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
172172
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
173+
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
173174
*/
174175
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
175176
try {
@@ -184,6 +185,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
184185
if (opt_config.includeUtm !== undefined) {
185186
this.options.includeUtm = !!opt_config.includeUtm;
186187
}
188+
if (opt_config.includeReferrer !== undefined) {
189+
this.options.includeReferrer = !!opt_config.includeReferrer;
190+
}
187191
this.options.platform = opt_config.platform || this.options.platform;
188192
this.options.language = opt_config.language || this.options.language;
189193
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
@@ -312,6 +316,18 @@ Amplitude.prototype._initUtmData = function(queryParams, cookieParams) {
312316
this._utmProperties = Amplitude._getUtmData(cookieParams, queryParams);
313317
};
314318

319+
Amplitude.prototype._getReferrer = function() {
320+
return document.referrer;
321+
};
322+
323+
Amplitude.prototype._getReferringDomain = function() {
324+
var parts = this._getReferrer().split("/");
325+
if (parts.length >= 3) {
326+
return parts[2];
327+
}
328+
return "";
329+
};
330+
315331
Amplitude.prototype.saveEvents = function() {
316332
try {
317333
localStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));
@@ -407,11 +423,20 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
407423
localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
408424
localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId);
409425

410-
// Add the utm properties, if any, onto the user properties.
411426
var userProperties = {};
412427
object.merge(userProperties, this.options.userProperties || {});
428+
429+
// Add the utm properties, if any, onto the user properties.
413430
object.merge(userProperties, this._utmProperties);
414431

432+
// Add referral info onto the user properties
433+
if (this.options.includeReferrer && this._getReferrer()) {
434+
object.merge(userProperties, {
435+
'referrer': this._getReferrer(),
436+
'referring_domain': this._getReferringDomain()
437+
});
438+
}
439+
415440
apiProperties = apiProperties || {};
416441
eventProperties = eventProperties || {};
417442
var event = {

amplitude.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/amplitude.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Amplitude.prototype._newSession = false;
5858
* opt_config Configuration options
5959
* - saveEvents (boolean) Whether to save events to local storage. Defaults to true.
6060
* - includeUtm (boolean) Whether to send utm parameters with events. Defaults to false.
61+
* - includeReferrer (boolean) Whether to send referrer info with events. Defaults to false.
6162
*/
6263
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
6364
try {
@@ -72,6 +73,9 @@ Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
7273
if (opt_config.includeUtm !== undefined) {
7374
this.options.includeUtm = !!opt_config.includeUtm;
7475
}
76+
if (opt_config.includeReferrer !== undefined) {
77+
this.options.includeReferrer = !!opt_config.includeReferrer;
78+
}
7579
this.options.platform = opt_config.platform || this.options.platform;
7680
this.options.language = opt_config.language || this.options.language;
7781
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
@@ -200,6 +204,18 @@ Amplitude.prototype._initUtmData = function(queryParams, cookieParams) {
200204
this._utmProperties = Amplitude._getUtmData(cookieParams, queryParams);
201205
};
202206

207+
Amplitude.prototype._getReferrer = function() {
208+
return document.referrer;
209+
};
210+
211+
Amplitude.prototype._getReferringDomain = function() {
212+
var parts = this._getReferrer().split("/");
213+
if (parts.length >= 3) {
214+
return parts[2];
215+
}
216+
return "";
217+
};
218+
203219
Amplitude.prototype.saveEvents = function() {
204220
try {
205221
localStorage.setItem(this.options.unsentKey, JSON.stringify(this._unsentEvents));
@@ -295,11 +311,20 @@ Amplitude.prototype._logEvent = function(eventType, eventProperties, apiProperti
295311
localStorage.setItem(LocalStorageKeys.LAST_EVENT_TIME, this._lastEventTime);
296312
localStorage.setItem(LocalStorageKeys.LAST_EVENT_ID, eventId);
297313

298-
// Add the utm properties, if any, onto the user properties.
299314
var userProperties = {};
300315
object.merge(userProperties, this.options.userProperties || {});
316+
317+
// Add the utm properties, if any, onto the user properties.
301318
object.merge(userProperties, this._utmProperties);
302319

320+
// Add referral info onto the user properties
321+
if (this.options.includeReferrer && this._getReferrer()) {
322+
object.merge(userProperties, {
323+
'referrer': this._getReferrer(),
324+
'referring_domain': this._getReferringDomain()
325+
});
326+
}
327+
303328
apiProperties = apiProperties || {};
304329
eventProperties = eventProperties || {};
305330
var event = {

test/amplitude.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,58 @@ describe('Amplitude', function() {
490490
});
491491
});
492492

493+
describe('gatherReferrer', function() {
494+
beforeEach(function() {
495+
amplitude.init(apiKey);
496+
sinon.stub(amplitude, '_getReferrer').returns('https://amplitude.com/contact');
497+
});
498+
499+
afterEach(function() {
500+
reset();
501+
});
502+
503+
it('should not send referrer data when the includeReferrer flag is false', function() {
504+
amplitude.init(apiKey, undefined, {});
505+
506+
amplitude.setUserProperties({user_prop: true});
507+
amplitude.logEvent('Referrer Test Event', {});
508+
assert.lengthOf(server.requests, 1);
509+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
510+
assert.equal(events[0].user_properties.referrer, undefined);
511+
assert.equal(events[0].user_properties.referring_domain, undefined);
512+
});
513+
514+
it('should send referrer data when the includeReferrer flag is true', function() {
515+
reset();
516+
amplitude.init(apiKey, undefined, {includeReferrer: true});
517+
518+
amplitude.logEvent('Referrer Test Event', {});
519+
520+
assert.lengthOf(server.requests, 1);
521+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
522+
assert.deepEqual(events[0].user_properties, {
523+
referrer: 'https://amplitude.com/contact',
524+
referring_domain: 'amplitude.com'
525+
});
526+
});
527+
528+
it('should add referrer data to the user properties', function() {
529+
reset();
530+
amplitude.init(apiKey, undefined, {includeReferrer: true});
531+
532+
amplitude.setUserProperties({user_prop: true});
533+
amplitude.logEvent('Referrer Test Event', {});
534+
535+
assert.lengthOf(server.requests, 1);
536+
var events = JSON.parse(querystring.parse(server.requests[0].requestBody).e);
537+
assert.deepEqual(events[0].user_properties, {
538+
user_prop: true,
539+
referrer: 'https://amplitude.com/contact',
540+
referring_domain: 'amplitude.com'
541+
});
542+
});
543+
});
544+
493545
describe('logRevenue', function() {
494546
beforeEach(function() {
495547
amplitude.init(apiKey);

0 commit comments

Comments
 (0)