@@ -131,8 +131,9 @@ var DEFAULT_OPTIONS = {
131
131
cookieExpiration : 365 * 10 ,
132
132
unsentKey : 'amplitude_unsent' ,
133
133
saveEvents : true ,
134
- domain : '' ,
135
- sessionTimeout : 30 * 60 * 1000
134
+ domain : undefined ,
135
+ sessionTimeout : 30 * 60 * 1000 ,
136
+ platform : 'Web'
136
137
} ;
137
138
var LocalStorageKeys = {
138
139
LAST_EVENT_ID : 'amplitude_lastEventId' ,
@@ -146,6 +147,7 @@ var LocalStorageKeys = {
146
147
var Amplitude = function ( ) {
147
148
this . _unsentEvents = [ ] ;
148
149
this . _ua = detect . parse ( navigator . userAgent ) ;
150
+ this . options = object . merge ( { } , DEFAULT_OPTIONS ) ;
149
151
} ;
150
152
151
153
@@ -163,15 +165,16 @@ Amplitude.prototype._sessionId = null;
163
165
*/
164
166
Amplitude . prototype . init = function ( apiKey , opt_userId , opt_config ) {
165
167
try {
166
- this . options = object . merge ( { } , DEFAULT_OPTIONS ) ;
167
168
this . options . apiKey = apiKey ;
168
169
if ( opt_config ) {
169
170
if ( opt_config . saveEvents !== undefined ) {
170
171
this . options . saveEvents = ! ! opt_config . saveEvents ;
171
172
}
172
173
if ( opt_config . domain !== undefined ) {
173
- this . options . saveEvents = opt_config . domain ;
174
+ this . options . domain = opt_config . domain ;
174
175
}
176
+ this . options . platform = opt_config . platform || this . options . platform ;
177
+ this . options . sessionTimeout = opt_config . sessionTimeout || this . options . sessionTimeout ;
175
178
}
176
179
177
180
Cookie . options ( {
@@ -326,7 +329,7 @@ Amplitude.prototype.logEvent = function(eventType, eventProperties) {
326
329
session_id : sessionId || - 1 ,
327
330
event_type : eventType ,
328
331
version_name : this . options . versionName || null ,
329
- platform : 'Web' ,
332
+ platform : this . options . platform ,
330
333
os_name : ua . browser . family ,
331
334
os_version : ua . browser . version ,
332
335
device_model : ua . os . family ,
@@ -576,18 +579,15 @@ var reset = function() {
576
579
577
580
578
581
var options = function ( opts ) {
579
- if ( ! opts ) {
582
+ if ( arguments . length === 0 ) {
580
583
return _options ;
581
584
}
582
- _options . expirationDays = opts . expirationDays ;
583
585
584
- var domain ;
585
- if ( opts . domain ) {
586
- domain = opts . domain ;
587
- } else {
588
- domain = '.' + topDomain ( window . location . href ) ;
589
- }
586
+ opts = opts || { } ;
590
587
588
+ _options . expirationDays = opts . expirationDays ;
589
+
590
+ var domain = ( opts . domain !== undefined ) ? opts . domain : '.' + topDomain ( window . location . href ) ;
591
591
var token = Math . random ( ) ;
592
592
_options . domain = domain ;
593
593
set ( 'amplitude_test' , token ) ;
@@ -599,10 +599,19 @@ var options = function(opts) {
599
599
_options . domain = domain ;
600
600
} ;
601
601
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
+
602
611
603
612
var get = function ( name ) {
604
613
try {
605
- var nameEq = name + '=' ;
614
+ var nameEq = _domainSpecific ( name ) + '=' ;
606
615
var ca = document . cookie . split ( ';' ) ;
607
616
var value = null ;
608
617
for ( var i = 0 ; i < ca . length ; i ++ ) {
@@ -628,7 +637,7 @@ var get = function(name) {
628
637
629
638
var set = function ( name , value ) {
630
639
try {
631
- _set ( name , Base64 . encode ( JSON . stringify ( value ) ) , _options ) ;
640
+ _set ( _domainSpecific ( name ) , Base64 . encode ( JSON . stringify ( value ) ) , _options ) ;
632
641
return true ;
633
642
} catch ( e ) {
634
643
return false ;
@@ -657,7 +666,7 @@ var _set = function(name, value, opts) {
657
666
658
667
var remove = function ( name ) {
659
668
try {
660
- _set ( name , null , _options ) ;
669
+ _set ( _domainSpecific ( name ) , null , _options ) ;
661
670
return true ;
662
671
} catch ( e ) {
663
672
return false ;
0 commit comments