@@ -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} ;
137138var LocalStorageKeys = {
138139 LAST_EVENT_ID : 'amplitude_lastEventId' ,
@@ -146,6 +147,7 @@ var LocalStorageKeys = {
146147var 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 */
164166Amplitude . 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
578581var 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
603612var 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
629638var 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
658667var 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 ;
0 commit comments