@@ -307,25 +307,54 @@ describe('Segment.io', function() {
307
307
analytics . assert ( ! object . context . amp ) ;
308
308
} ) ;
309
309
310
- it ( 'should set xid if available, is enabled , and context.traits is not defined' , function ( ) {
310
+ it ( 'should set xid from cookie , and context.traits is not defined' , function ( ) {
311
311
segment . cookie ( 'seg_xid' , 'test_id' ) ;
312
312
segment . options . crossDomainIdServers = [
313
313
'userdata.example1.com' ,
314
314
'xid.domain2.com' ,
315
315
'localhost'
316
316
] ;
317
+ segment . options . saveCrossDomainIdInLocalStorage = false ;
317
318
318
319
segment . normalize ( object ) ;
319
320
assert . equal ( object . context . traits . crossDomainId , 'test_id' ) ;
320
321
} ) ;
321
322
322
- it ( 'should set xid if available, is enabled , and context.traits is defined' , function ( ) {
323
+ it ( 'should set xid from cookie , and context.traits is defined' , function ( ) {
323
324
segment . cookie ( 'seg_xid' , 'test_id' ) ;
324
325
segment . options . crossDomainIdServers = [
325
326
'userdata.example1.com' ,
326
327
'xid.domain2.com' ,
327
328
'localhost'
328
329
] ;
330
+ segment . options . saveCrossDomainIdInLocalStorage = false ;
331
+
332
+ var msg = { context :
{ traits :
{ email :
'[email protected] ' } } } ;
333
+ segment . normalize ( msg ) ;
334
+ assert . equal ( msg . context . traits . crossDomainId , 'test_id' ) ;
335
+ } ) ;
336
+
337
+ it ( 'should set xid from localStorage, and context.traits is not defined' , function ( ) {
338
+ window . localStorage . setItem ( 'seg_xid' , 'test_id' ) ;
339
+ segment . options . crossDomainIdServers = [
340
+ 'userdata.example1.com' ,
341
+ 'xid.domain2.com' ,
342
+ 'localhost'
343
+ ] ;
344
+ segment . options . saveCrossDomainIdInLocalStorage = true ;
345
+
346
+ segment . normalize ( object ) ;
347
+ assert . equal ( object . context . traits . crossDomainId , 'test_id' ) ;
348
+ } ) ;
349
+
350
+ it ( 'should set xid from localStorage, is enabled, and context.traits is defined' , function ( ) {
351
+ window . localStorage . setItem ( 'seg_xid' , 'test_id' ) ;
352
+ segment . options . crossDomainIdServers = [
353
+ 'userdata.example1.com' ,
354
+ 'xid.domain2.com' ,
355
+ 'localhost'
356
+ ] ;
357
+ segment . options . saveCrossDomainIdInLocalStorage = true ;
329
358
330
359
var msg = { context :
{ traits :
{ email :
'[email protected] ' } } } ;
331
360
segment . normalize ( msg ) ;
@@ -998,16 +1027,24 @@ describe('Segment.io', function() {
998
1027
} ) ;
999
1028
1000
1029
it ( 'should obtain crossDomainId' , function ( ) {
1001
- var res = null ;
1002
- segment . retrieveCrossDomainId ( function ( err , response ) {
1003
- res = response ;
1004
- } ) ;
1005
1030
server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
1006
1031
200 ,
1007
1032
{ 'Content-Type' : 'application/json' } ,
1008
1033
'{ "id": "xdomain-id-1" }'
1009
1034
] ) ;
1010
- server . respond ( ) ;
1035
+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1036
+ server . respondWith ( 'GET' , 'https://localhost/v1/saveId?writeKey=' + segment . options . apiKey + '&xid=xdomain-id-1' , [
1037
+ 200 ,
1038
+ { 'Content-Type' : 'text/plan' } ,
1039
+ 'OK'
1040
+ ] ) ;
1041
+ }
1042
+ server . respondImmediately = true ;
1043
+
1044
+ var res = null ;
1045
+ segment . retrieveCrossDomainId ( function ( err , response ) {
1046
+ res = response ;
1047
+ } ) ;
1011
1048
1012
1049
var identify = segment . onidentify . args [ 0 ] ;
1013
1050
analytics . assert ( identify [ 0 ] . traits ( ) . crossDomainId === 'xdomain-id-1' ) ;
@@ -1019,11 +1056,6 @@ describe('Segment.io', function() {
1019
1056
} ) ;
1020
1057
1021
1058
it ( 'should generate crossDomainId if no server has it' , function ( ) {
1022
- var res = null ;
1023
- segment . retrieveCrossDomainId ( function ( err , response ) {
1024
- res = response ;
1025
- } ) ;
1026
-
1027
1059
server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
1028
1060
200 ,
1029
1061
{ 'Content-Type' : 'application/json' } ,
@@ -1034,7 +1066,19 @@ describe('Segment.io', function() {
1034
1066
{ 'Content-Type' : 'application/json' } ,
1035
1067
'{ "id": null }'
1036
1068
] ) ;
1037
- server . respond ( ) ;
1069
+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1070
+ server . respondWith ( 'GET' , / h t t p s : \/ \/ l o c a l h o s t \/ v 1 \/ s a v e I d / , [
1071
+ 200 ,
1072
+ { 'Content-Type' : 'text/plan' } ,
1073
+ 'OK'
1074
+ ] ) ;
1075
+ }
1076
+ server . respondImmediately = true ;
1077
+
1078
+ var res = null ;
1079
+ segment . retrieveCrossDomainId ( function ( err , response ) {
1080
+ res = response ;
1081
+ } ) ;
1038
1082
1039
1083
var identify = segment . onidentify . args [ 0 ] ;
1040
1084
var crossDomainId = identify [ 0 ] . traits ( ) . crossDomainId ;
@@ -1103,13 +1147,6 @@ describe('Segment.io', function() {
1103
1147
} ) ;
1104
1148
1105
1149
it ( 'should succeed even if one server fails' , function ( ) {
1106
- var err = null ;
1107
- var res = null ;
1108
- segment . retrieveCrossDomainId ( function ( error , response ) {
1109
- err = error ;
1110
- res = response ;
1111
- } ) ;
1112
-
1113
1150
server . respondWith ( 'GET' , 'https://xid.domain2.com/v1/id/' + segment . options . apiKey , [
1114
1151
500 ,
1115
1152
{ 'Content-Type' : 'application/json' } ,
@@ -1120,7 +1157,21 @@ describe('Segment.io', function() {
1120
1157
{ 'Content-Type' : 'application/json' } ,
1121
1158
'{ "id": "xidxid" }'
1122
1159
] ) ;
1123
- server . respond ( ) ;
1160
+ if ( segment . options . saveCrossDomainIdInLocalStorage ) {
1161
+ server . respondWith ( 'GET' , 'https://localhost/v1/saveId?writeKey=' + segment . options . apiKey + '&xid=xidxid' , [
1162
+ 200 ,
1163
+ { 'Content-Type' : 'text/plan' } ,
1164
+ 'OK'
1165
+ ] ) ;
1166
+ }
1167
+ server . respondImmediately = true ;
1168
+
1169
+ var err = null ;
1170
+ var res = null ;
1171
+ segment . retrieveCrossDomainId ( function ( error , response ) {
1172
+ err = error ;
1173
+ res = response ;
1174
+ } ) ;
1124
1175
1125
1176
var identify = segment . onidentify . args [ 0 ] ;
1126
1177
analytics . assert ( identify [ 0 ] . traits ( ) . crossDomainId === 'xidxid' ) ;
0 commit comments