@@ -57,3 +57,139 @@ Scenario('Login as a different user', async (I, testID) => {
5757 assert . strictEqual ( lsUserId , null ) ;
5858 await I . stopRecording ( testID ) ;
5959} ) . injectDependencies ( { testID : 'login-as-different-user' } ) ;
60+
61+ Scenario ( 'Set anonymous ID explicitly' , async ( I , testID ) => {
62+ async function assertAnonymousIDInStorage ( expected ) {
63+ // Checks if the value in cookie and local storage are expected
64+ let cookie = await I . grabCookie ( 'ajs_anonymous_id' ) ;
65+ assert . strictEqual (
66+ cookie . value ,
67+ `%22${ expected } %22` ,
68+ 'unexpected cookie value'
69+ ) ;
70+ assert . strictEqual (
71+ await I . executeScript ( ( ) => localStorage . getItem ( 'ajs_anonymous_id' ) ) ,
72+ `"${ expected } "` ,
73+ 'unexpected LS value'
74+ ) ;
75+ }
76+ I . loadAJS ( { local : true } ) ;
77+
78+ I . startRecording ( testID ) ;
79+ const [ anonymousId1 , anonymousId2 ] = await I . executeScript ( ( ) => {
80+ return [ analytics . user ( ) . anonymousId ( ) , analytics . user ( ) . anonymousId ( ) ] ;
81+ } ) ;
82+ assert ( anonymousId1 != null , 'AnonymousID should not be null' ) ;
83+ assert . strictEqual (
84+ anonymousId1 ,
85+ anonymousId2 ,
86+ 'Calling anonymousId twice should return the same ID'
87+ ) ;
88+ await assertAnonymousIDInStorage ( anonymousId1 ) ;
89+
90+ // Use different methods to set anonymousId; network request should reflect each of the IDs
91+
92+ // via anonymousId()
93+ await I . executeScript ( ( ) => {
94+ analytics . user ( ) . anonymousId ( 'first-id' ) ;
95+ } ) ;
96+ I . click ( '#track-checkout-started' ) ;
97+ await assertAnonymousIDInStorage ( 'first-id' ) ;
98+
99+ // via options
100+ await I . executeScript ( ( ) => {
101+ analytics . page ( { } , { anonymousId : 'second-id' } ) ;
102+ } ) ;
103+ I . click ( '#track-checkout-started' ) ;
104+ await assertAnonymousIDInStorage ( 'second-id' ) ;
105+
106+ // via setAnonymousId()
107+ await I . executeScript ( ( ) => {
108+ analytics . setAnonymousId ( 'third-id' ) ;
109+ } ) ;
110+ I . click ( '#track-checkout-started' ) ;
111+ await assertAnonymousIDInStorage ( 'third-id' ) ;
112+ await I . stopRecording ( testID ) ;
113+ } ) . injectDependencies ( { testID : 'set-anonymous-id' } ) ;
114+
115+ Scenario ( 'Selective destination filtering' , async ( I , testID ) => {
116+ I . loadAJS ( { local : true } ) ;
117+ I . startRecording ( testID ) ;
118+ I . click ( '#page-home' ) ;
119+ await I . executeScript ( ( ) => {
120+ analytics . identify (
121+ 'user_123' ,
122+ {
123+ email : 'jane.kim@example.com' ,
124+ name : 'Jane Kim'
125+ } ,
126+ {
127+ integrations : {
128+ All : false ,
129+ Intercom : true ,
130+ 'Google Analytics' : true
131+ }
132+ }
133+ ) ;
134+ } ) ;
135+ I . click ( '#track-checkout-started' ) ;
136+ await I . executeScript ( ( ) => {
137+ analytics . identify (
138+ 'user_123' ,
139+ {
140+ email : 'jane.kim@example.com' ,
141+ name : 'Jane Kim'
142+ } ,
143+ {
144+ integrations : {
145+ Intercom : false ,
146+ 'Google Analytics' : false
147+ }
148+ }
149+ ) ;
150+ } ) ;
151+ I . click ( '#page-home' ) ;
152+ await I . stopRecording ( testID ) ;
153+ } ) . injectDependencies ( { testID : 'selective-destination-filtering' } ) ;
154+
155+ Scenario ( 'Traits can be cached and cleared' , async ( I , testID ) => {
156+ I . loadAJS ( { local : true } ) ;
157+ I . startRecording ( testID ) ;
158+ I . click ( '#page-home' ) ;
159+ const [ actualTraits1 , actualTraits2 ] = await I . executeScript ( ( ) => {
160+ // identify user with traits
161+ analytics . identify ( 'user_123' , {
162+ email : 'jane.kim@example.com' ,
163+ name : 'Jane Kim'
164+ } ) ;
165+ const traits1 = analytics . user ( ) . traits ( ) ;
166+ // clear the traits
167+ analytics . user ( ) . traits ( { } ) ;
168+ const traits2 = analytics . user ( ) . traits ( ) ;
169+ return [ traits1 , traits2 ] ;
170+ } ) ;
171+ assert . deepEqual (
172+ actualTraits1 ,
173+ {
174+ email : 'jane.kim@example.com' ,
175+ name : 'Jane Kim'
176+ } ,
177+ 'expected traits to be cached'
178+ ) ;
179+ assert . deepEqual ( actualTraits2 , { } , 'expected empty traits after clearing' ) ;
180+ I . click ( '#page-home' ) ;
181+ await I . stopRecording ( testID ) ;
182+ } ) . injectDependencies ( { testID : 'traits-cached-and-cleared' } ) ;
183+
184+ Scenario ( 'Anonymizing IP address' , async ( I , testID ) => {
185+ I . loadAJS ( { local : true } ) ;
186+ I . startRecording ( testID ) ;
187+ I . click ( '#page-home' ) ;
188+ await I . executeScript ( ( ) => {
189+ // network request for this track call should contain this context object
190+ // to prevent recording IP address associated with the request
191+ analytics . track ( 'Order Completed' , { } , { context : { ip : '0.0.0.0' } } ) ;
192+ } ) ;
193+ I . click ( '#page-home' ) ;
194+ await I . stopRecording ( testID ) ;
195+ } ) . injectDependencies ( { testID : 'anonymizing-ip-address' } ) ;
0 commit comments