@@ -162,7 +162,7 @@ describe('AmplitudeClient', function() {
162162
163163 var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
164164 amplitude2 . init ( apiKey ) ;
165- assert . notEqual ( amplitude . options . deviceId , deviceId ) ;
165+ assert . notEqual ( amplitude2 . options . deviceId , deviceId ) ;
166166 assert . isNull ( amplitude2 . options . userId ) ;
167167 assert . isFalse ( amplitude2 . options . optOut ) ;
168168
@@ -396,6 +396,35 @@ describe('AmplitudeClient', function() {
396396 assert . equal ( localStorage . getItem ( 'amplitude_unsent_identify' ) , existingIdentify ) ;
397397 } ) ;
398398
399+ it ( 'should load saved events for non-default instances' , function ( ) {
400+ var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
401+ '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
402+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
403+ '"event_properties":{},"user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
404+ ':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130, "groups":{}}]' ;
405+ var existingIdentify = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769338995,' +
406+ '"event_id":82,"session_id":1453763315544,"event_type":"$identify","version_name":"Web","platform":"Web"' +
407+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
408+ '"event_properties":{},"user_properties":{"$set":{"age":30,"city":"San Francisco, CA"}},"uuid":"' +
409+ 'c50e1be4-7976-436a-aa25-d9ee38951082","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number"' +
410+ ':131, "groups":{}}]' ;
411+ localStorage . setItem ( 'amplitude_unsent_new_app' , existingEvent ) ;
412+ localStorage . setItem ( 'amplitude_unsent_identify_new_app' , existingIdentify ) ;
413+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent' ) ) ;
414+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent_identify' ) ) ;
415+
416+ var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
417+ amplitude2 . init ( apiKey , null , { batchEvents : true } ) ;
418+
419+ // check event loaded into memory
420+ assert . deepEqual ( amplitude2 . _unsentEvents , JSON . parse ( existingEvent ) ) ;
421+ assert . deepEqual ( amplitude2 . _unsentIdentifys , JSON . parse ( existingIdentify ) ) ;
422+
423+ // check local storage keys are still same
424+ assert . equal ( localStorage . getItem ( 'amplitude_unsent_new_app' ) , existingEvent ) ;
425+ assert . equal ( localStorage . getItem ( 'amplitude_unsent_identify_new_app' ) , existingIdentify ) ;
426+ } ) ;
427+
399428 it ( 'should validate event properties when loading saved events from localStorage' , function ( ) {
400429 var existingEvents = '[{"device_id":"15a82aaa-0d9e-4083-a32d-2352191877e6","user_id":"15a82aaa-0d9e-4083-a32d' +
401430 '-2352191877e6","timestamp":1455744744413,"event_id":2,"session_id":1455744733865,"event_type":"clicked",' +
@@ -457,7 +486,7 @@ describe('AmplitudeClient', function() {
457486 assert . deepEqual ( amplitude2 . _unsentIdentifys [ 0 ] . user_properties , { '$set' : expected } ) ;
458487 } ) ;
459488
460- it ( 'should load saved events from localStorage new keys and send events' , function ( ) {
489+ it ( 'should load saved events from localStorage and send events for default instance ' , function ( ) {
461490 var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
462491 '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
463492 ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
@@ -493,6 +522,42 @@ describe('AmplitudeClient', function() {
493522 assert . equal ( events [ 1 ] . event_type , '$identify' ) ;
494523 } ) ;
495524
525+ it ( 'should load saved events from localStorage new keys and send events' , function ( ) {
526+ var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
527+ '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
528+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
529+ '"event_properties":{},"user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
530+ ':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130}]' ;
531+ var existingIdentify = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769338995,' +
532+ '"event_id":82,"session_id":1453763315544,"event_type":"$identify","version_name":"Web","platform":"Web"' +
533+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
534+ '"event_properties":{},"user_properties":{"$set":{"age":30,"city":"San Francisco, CA"}},"uuid":"' +
535+ 'c50e1be4-7976-436a-aa25-d9ee38951082","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number"' +
536+ ':131}]' ;
537+ localStorage . setItem ( 'amplitude_unsent_new_app' , existingEvent ) ;
538+ localStorage . setItem ( 'amplitude_unsent_identify_new_app' , existingIdentify ) ;
539+
540+ var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
541+ amplitude2 . init ( apiKey , null , { batchEvents : true , eventUploadThreshold : 2 } ) ;
542+ server . respondWith ( 'success' ) ;
543+ server . respond ( ) ;
544+
545+ // check event loaded into memory
546+ assert . deepEqual ( amplitude2 . _unsentEvents , [ ] ) ;
547+ assert . deepEqual ( amplitude2 . _unsentIdentifys , [ ] ) ;
548+
549+ // check local storage keys are still same
550+ assert . equal ( localStorage . getItem ( 'amplitude_unsent_new_app' ) , JSON . stringify ( [ ] ) ) ;
551+ assert . equal ( localStorage . getItem ( 'amplitude_unsent_identify_new_app' ) , JSON . stringify ( [ ] ) ) ;
552+
553+ // check request
554+ assert . lengthOf ( server . requests , 1 ) ;
555+ var events = JSON . parse ( querystring . parse ( server . requests [ 0 ] . requestBody ) . e ) ;
556+ assert . lengthOf ( events , 2 ) ;
557+ assert . equal ( events [ 0 ] . event_id , 49 ) ;
558+ assert . equal ( events [ 1 ] . event_type , '$identify' ) ;
559+ } ) ;
560+
496561 it ( 'should validate event properties when loading saved events from localStorage' , function ( ) {
497562 var existingEvents = '[{"device_id":"15a82aaa-0d9e-4083-a32d-2352191877e6","user_id":"15a82aaa-0d9e-4083-a32d' +
498563 '-2352191877e6","timestamp":1455744744413,"event_id":2,"session_id":1455744733865,"event_type":"clicked",' +
@@ -536,6 +601,40 @@ describe('AmplitudeClient', function() {
536601 assert . deepEqual ( amplitude2 . _unsentEvents [ 0 ] . event_properties , { } ) ;
537602 assert . deepEqual ( amplitude2 . _unsentEvents [ 1 ] . event_properties , expected ) ;
538603 } ) ;
604+
605+ it ( 'should not load saved events from another instances\'s localStorage' , function ( ) {
606+ var existingEvent = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769146589,' +
607+ '"event_id":49,"session_id":1453763315544,"event_type":"clicked","version_name":"Web","platform":"Web"' +
608+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
609+ '"event_properties":{},"user_properties":{},"uuid":"3c508faa-a5c9-45fa-9da7-9f4f3b992fb0","library"' +
610+ ':{"name":"amplitude-js","version":"2.9.0"},"sequence_number":130}]' ;
611+ var existingIdentify = '[{"device_id":"test_device_id","user_id":"test_user_id","timestamp":1453769338995,' +
612+ '"event_id":82,"session_id":1453763315544,"event_type":"$identify","version_name":"Web","platform":"Web"' +
613+ ',"os_name":"Chrome","os_version":"47","device_model":"Mac","language":"en-US","api_properties":{},' +
614+ '"event_properties":{},"user_properties":{"$set":{"age":30,"city":"San Francisco, CA"}},"uuid":"' +
615+ 'c50e1be4-7976-436a-aa25-d9ee38951082","library":{"name":"amplitude-js","version":"2.9.0"},"sequence_number"' +
616+ ':131}]' ;
617+ localStorage . setItem ( 'amplitude_unsent' , existingEvent ) ;
618+ localStorage . setItem ( 'amplitude_unsent_identify' , existingIdentify ) ;
619+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent_new_app' ) ) ;
620+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent_identify_new_app' ) ) ;
621+
622+ var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
623+ amplitude2 . init ( apiKey , null , { batchEvents : true , eventUploadThreshold : 2 } ) ;
624+
625+ // check events not loaded into memory
626+ assert . deepEqual ( amplitude2 . _unsentEvents , [ ] ) ;
627+ assert . deepEqual ( amplitude2 . _unsentIdentifys , [ ] ) ;
628+
629+ // check local storage
630+ assert . equal ( localStorage . getItem ( 'amplitude_unsent' ) , existingEvent ) ;
631+ assert . equal ( localStorage . getItem ( 'amplitude_unsent_identify' ) , existingIdentify ) ;
632+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent_new_app' ) ) ;
633+ assert . isNull ( localStorage . getItem ( 'amplitude_unsent_identify_new_app' ) ) ;
634+
635+ // check request
636+ assert . lengthOf ( server . requests , 0 ) ;
637+ } ) ;
539638 } ) ;
540639
541640 describe ( 'runQueuedFunctions' , function ( ) {
@@ -2188,6 +2287,46 @@ describe('setVersionName', function() {
21882287 // existing value persists
21892288 assert . equal ( sessionStorage . getItem ( 'amplitude_referrer' ) , 'https://www.google.com/search?' ) ;
21902289 } ) ;
2290+
2291+ it ( 'should not override any existing referrer values in session storage for non-default instances' , function ( ) {
2292+ reset ( ) ;
2293+ sessionStorage . setItem ( 'amplitude_referrer_new_app' , 'https://www.google.com/search?' ) ;
2294+ var amplitude2 = new AmplitudeClient ( 'new_app' ) ;
2295+ sinon . stub ( amplitude2 , '_getReferrer' ) . returns ( 'https://amplitude.com/contact' ) ;
2296+ amplitude2 . init ( apiKey , undefined , { includeReferrer : true , batchEvents : true , eventUploadThreshold : 3 } ) ;
2297+ amplitude2 . _getReferrer . restore ( ) ;
2298+
2299+ amplitude2 . _saveReferrer ( 'https://facebook.com/contact' ) ;
2300+ amplitude2 . logEvent ( 'Referrer Test Event' , { } ) ;
2301+ assert . lengthOf ( server . requests , 1 ) ;
2302+ var events = JSON . parse ( querystring . parse ( server . requests [ 0 ] . requestBody ) . e ) ;
2303+ assert . lengthOf ( events , 3 ) ;
2304+
2305+ // first event should be identify with initial_referrer and NO referrer
2306+ assert . equal ( events [ 0 ] . event_type , '$identify' ) ;
2307+ assert . deepEqual ( events [ 0 ] . user_properties , {
2308+ '$setOnce' : {
2309+ 'initial_referrer' : 'https://amplitude.com/contact' ,
2310+ 'initial_referring_domain' : 'amplitude.com'
2311+ }
2312+ } ) ;
2313+
2314+ // second event should be another identify but with the new referrer
2315+ assert . equal ( events [ 1 ] . event_type , '$identify' ) ;
2316+ assert . deepEqual ( events [ 1 ] . user_properties , {
2317+ '$setOnce' : {
2318+ 'initial_referrer' : 'https://facebook.com/contact' ,
2319+ 'initial_referring_domain' : 'facebook.com'
2320+ }
2321+ } ) ;
2322+
2323+ // third event should be the test event with no referrer information
2324+ assert . equal ( events [ 2 ] . event_type , 'Referrer Test Event' ) ;
2325+ assert . deepEqual ( events [ 2 ] . user_properties , { } ) ;
2326+
2327+ // existing value persists
2328+ assert . equal ( sessionStorage . getItem ( 'amplitude_referrer_new_app' ) , 'https://www.google.com/search?' ) ;
2329+ } ) ;
21912330 } ) ;
21922331
21932332 describe ( 'logRevenue' , function ( ) {
0 commit comments