Skip to content

Commit c9ee486

Browse files
committed
Modify cookies for reverse compatibility
1 parent 83e1a20 commit c9ee486

File tree

4 files changed

+52
-34
lines changed

4 files changed

+52
-34
lines changed

amplitude.js

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,9 @@ var DEFAULT_OPTIONS = {
131131
cookieExpiration: 365 * 10,
132132
unsentKey: 'amplitude_unsent',
133133
saveEvents: true,
134-
domain: '',
135-
sessionTimeout: 30 * 60 * 1000
134+
domain: undefined,
135+
sessionTimeout: 30 * 60 * 1000,
136+
platform: 'Web'
136137
};
137138
var LocalStorageKeys = {
138139
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -146,6 +147,7 @@ var LocalStorageKeys = {
146147
var Amplitude = function() {
147148
this._unsentEvents = [];
148149
this._ua = detect.parse(navigator.userAgent);
150+
this.options = object.merge({}, DEFAULT_OPTIONS);
149151
};
150152

151153

@@ -163,15 +165,16 @@ Amplitude.prototype._sessionId = null;
163165
*/
164166
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
165167
try {
166-
this.options = object.merge({}, DEFAULT_OPTIONS);
167168
this.options.apiKey = apiKey;
168169
if (opt_config) {
169170
if (opt_config.saveEvents !== undefined) {
170171
this.options.saveEvents = !!opt_config.saveEvents;
171172
}
172173
if (opt_config.domain !== undefined) {
173-
this.options.saveEvents = opt_config.domain;
174+
this.options.domain = opt_config.domain;
174175
}
176+
this.options.platform = opt_config.platform || this.options.platform;
177+
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
175178
}
176179

177180
Cookie.options({
@@ -326,7 +329,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
326329
session_id: sessionId || -1,
327330
event_type: eventType,
328331
version_name: this.options.versionName || null,
329-
platform: 'Web',
332+
platform: this.options.platform,
330333
os_name: ua.browser.family,
331334
os_version: ua.browser.version,
332335
device_model: ua.os.family,
@@ -576,18 +579,15 @@ var reset = function() {
576579

577580

578581
var options = function(opts) {
579-
if (!opts) {
582+
if (arguments.length === 0) {
580583
return _options;
581584
}
582-
_options.expirationDays = opts.expirationDays;
583585

584-
var domain;
585-
if (opts.domain) {
586-
domain = opts.domain;
587-
} else {
588-
domain = '.' + topDomain(window.location.href);
589-
}
586+
opts = opts || {};
590587

588+
_options.expirationDays = opts.expirationDays;
589+
590+
var domain = (opts.domain !== undefined) ? opts.domain : '.' + topDomain(window.location.href);
591591
var token = Math.random();
592592
_options.domain = domain;
593593
set('amplitude_test', token);
@@ -599,10 +599,19 @@ var options = function(opts) {
599599
_options.domain = domain;
600600
};
601601

602+
var _domainSpecific = function(name) {
603+
// differentiate between cookies on different domains
604+
var suffix = '';
605+
if (_options.domain) {
606+
suffix = _options.domain.charAt(0) == '.' ? _options.domain.substring(1) : _options.domain;
607+
}
608+
return name + suffix;
609+
};
610+
602611

603612
var get = function(name) {
604613
try {
605-
var nameEq = name + '=';
614+
var nameEq = _domainSpecific(name) + '=';
606615
var ca = document.cookie.split(';');
607616
var value = null;
608617
for (var i = 0; i < ca.length; i++) {
@@ -628,7 +637,7 @@ var get = function(name) {
628637

629638
var set = function(name, value) {
630639
try {
631-
_set(name, Base64.encode(JSON.stringify(value)), _options);
640+
_set(_domainSpecific(name), Base64.encode(JSON.stringify(value)), _options);
632641
return true;
633642
} catch (e) {
634643
return false;
@@ -657,7 +666,7 @@ var _set = function(name, value, opts) {
657666

658667
var remove = function(name) {
659668
try {
660-
_set(name, null, _options);
669+
_set(_domainSpecific(name), null, _options);
661670
return true;
662671
} catch (e) {
663672
return false;

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: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ var DEFAULT_OPTIONS = {
2121
cookieExpiration: 365 * 10,
2222
unsentKey: 'amplitude_unsent',
2323
saveEvents: true,
24-
domain: '',
25-
sessionTimeout: 30 * 60 * 1000
24+
domain: undefined,
25+
sessionTimeout: 30 * 60 * 1000,
26+
platform: 'Web'
2627
};
2728
var LocalStorageKeys = {
2829
LAST_EVENT_ID: 'amplitude_lastEventId',
@@ -36,6 +37,7 @@ var LocalStorageKeys = {
3637
var Amplitude = function() {
3738
this._unsentEvents = [];
3839
this._ua = detect.parse(navigator.userAgent);
40+
this.options = object.merge({}, DEFAULT_OPTIONS);
3941
};
4042

4143

@@ -53,15 +55,16 @@ Amplitude.prototype._sessionId = null;
5355
*/
5456
Amplitude.prototype.init = function(apiKey, opt_userId, opt_config) {
5557
try {
56-
this.options = object.merge({}, DEFAULT_OPTIONS);
5758
this.options.apiKey = apiKey;
5859
if (opt_config) {
5960
if (opt_config.saveEvents !== undefined) {
6061
this.options.saveEvents = !!opt_config.saveEvents;
6162
}
6263
if (opt_config.domain !== undefined) {
63-
this.options.saveEvents = opt_config.domain;
64+
this.options.domain = opt_config.domain;
6465
}
66+
this.options.platform = opt_config.platform || this.options.platform;
67+
this.options.sessionTimeout = opt_config.sessionTimeout || this.options.sessionTimeout;
6568
}
6669

6770
Cookie.options({
@@ -216,7 +219,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
216219
session_id: sessionId || -1,
217220
event_type: eventType,
218221
version_name: this.options.versionName || null,
219-
platform: 'Web',
222+
platform: this.options.platform,
220223
os_name: ua.browser.family,
221224
os_version: ua.browser.version,
222225
device_model: ua.os.family,

src/cookie.js

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,15 @@ var reset = function() {
2020

2121

2222
var options = function(opts) {
23-
if (!opts) {
23+
if (arguments.length === 0) {
2424
return _options;
2525
}
26-
_options.expirationDays = opts.expirationDays;
2726

28-
var domain;
29-
if (opts.domain) {
30-
domain = opts.domain;
31-
} else {
32-
domain = '.' + topDomain(window.location.href);
33-
}
27+
opts = opts || {};
28+
29+
_options.expirationDays = opts.expirationDays;
3430

31+
var domain = (opts.domain !== undefined) ? opts.domain : '.' + topDomain(window.location.href);
3532
var token = Math.random();
3633
_options.domain = domain;
3734
set('amplitude_test', token);
@@ -43,10 +40,19 @@ var options = function(opts) {
4340
_options.domain = domain;
4441
};
4542

43+
var _domainSpecific = function(name) {
44+
// differentiate between cookies on different domains
45+
var suffix = '';
46+
if (_options.domain) {
47+
suffix = _options.domain.charAt(0) == '.' ? _options.domain.substring(1) : _options.domain;
48+
}
49+
return name + suffix;
50+
};
51+
4652

4753
var get = function(name) {
4854
try {
49-
var nameEq = name + '=';
55+
var nameEq = _domainSpecific(name) + '=';
5056
var ca = document.cookie.split(';');
5157
var value = null;
5258
for (var i = 0; i < ca.length; i++) {
@@ -72,7 +78,7 @@ var get = function(name) {
7278

7379
var set = function(name, value) {
7480
try {
75-
_set(name, Base64.encode(JSON.stringify(value)), _options);
81+
_set(_domainSpecific(name), Base64.encode(JSON.stringify(value)), _options);
7682
return true;
7783
} catch (e) {
7884
return false;
@@ -101,7 +107,7 @@ var _set = function(name, value, opts) {
101107

102108
var remove = function(name) {
103109
try {
104-
_set(name, null, _options);
110+
_set(_domainSpecific(name), null, _options);
105111
return true;
106112
} catch (e) {
107113
return false;

0 commit comments

Comments
 (0)