@@ -5,6 +5,7 @@ import AnalyticsCore, {
55 FromParameterType ,
66 type IntegrationOptions ,
77 integrations ,
8+ PageType ,
89} from '@farfetch/blackout-analytics' ;
910import TestStorage from 'test-storage' ;
1011import type { WebContext } from '../context.js' ;
@@ -123,10 +124,14 @@ describe('analytics web', () => {
123124 await analytics . page ( event , properties , eventContext ) ;
124125
125126 expect ( coreTrackSpy ) . toHaveBeenCalledWith (
126- analyticsTrackTypes . Page ,
127- event ,
128- properties ,
129- eventContext ,
127+ expect . objectContaining ( {
128+ type : analyticsTrackTypes . Page ,
129+ event,
130+ properties,
131+ context : expect . objectContaining ( {
132+ event : expect . objectContaining ( eventContext ) ,
133+ } ) ,
134+ } ) ,
130135 ) ;
131136
132137 // Allow the integration to run - this will trigger the flow to track the previously stored page() event
@@ -276,10 +281,130 @@ describe('analytics web', () => {
276281 await analytics . page ( mockEvent , mockEventProperties , mockEventContext ) ;
277282
278283 expect ( coreTrackSpy ) . toHaveBeenCalledWith (
279- analyticsTrackTypes . Page ,
280- mockEvent ,
281- mockEventProperties ,
282- mockEventContext ,
284+ expect . objectContaining ( {
285+ type : analyticsTrackTypes . Page ,
286+ event : mockEvent ,
287+ properties : mockEventProperties ,
288+ context : expect . objectContaining ( {
289+ event : expect . objectContaining ( mockEventContext ) ,
290+ } ) ,
291+ } ) ,
283292 ) ;
284293 } ) ;
294+
295+ describe ( 'Page Location Referrer Property' , ( ) => {
296+ // The 'page location referrer' property is significant within the analytics context, specifically concerning web pages.
297+ // It is essential to ensure, through various tests, that different scenarios verify the proper operation of references,
298+ // especially in the case of a Single Page Application (SPA).
299+
300+ const newAnalyticsIntance = ( ) =>
301+ new ( analytics . constructor as {
302+ new ( ) : typeof analytics ;
303+ } ) ( ) ;
304+ // @ts -expect-error
305+ const coreTrackSpy = jest . spyOn ( AnalyticsCore . prototype , 'trackInternal' ) ;
306+
307+ beforeEach ( ( ) => {
308+ coreTrackSpy . mockClear ( ) ;
309+ } ) ;
310+
311+ it ( 'Should retrieve pageLocationReferrer value from origin on first page view' , async ( ) => {
312+ const origin = 'www.example.com' ;
313+
314+ jest . spyOn ( document , 'referrer' , 'get' ) . mockReturnValueOnce ( origin ) ;
315+
316+ const analyticsClean = newAnalyticsIntance ( ) ;
317+
318+ await analyticsClean . page ( PageType . Homepage ) ;
319+
320+ expect ( coreTrackSpy ) . toHaveBeenCalledWith (
321+ expect . objectContaining ( {
322+ type : analyticsTrackTypes . Page ,
323+ event : PageType . Homepage ,
324+ context : expect . objectContaining ( {
325+ web : expect . objectContaining ( {
326+ pageLocationReferrer : origin ,
327+ } ) ,
328+ } ) ,
329+ } ) ,
330+ ) ;
331+ } ) ;
332+
333+ it ( 'Should set `pageLocationReferrer` value to the previous page instead of document.referrer after the first navigation event' , async ( ) => {
334+ const origin = 'www.example.com' ;
335+
336+ jest . spyOn ( document , 'referrer' , 'get' ) . mockReturnValueOnce ( origin ) ;
337+ window . location . href = `${ origin } /${ PageType . Homepage } ` ;
338+
339+ const analyticsClean = newAnalyticsIntance ( ) ;
340+
341+ await analyticsClean . page ( PageType . Homepage ) ;
342+
343+ expect ( coreTrackSpy ) . toHaveBeenCalledWith (
344+ expect . objectContaining ( {
345+ type : analyticsTrackTypes . Page ,
346+ event : PageType . Homepage ,
347+ context : expect . objectContaining ( {
348+ web : expect . objectContaining ( {
349+ pageLocationReferrer : origin ,
350+ } ) ,
351+ } ) ,
352+ } ) ,
353+ ) ;
354+
355+ // set another page location
356+ window . location . href = `${ origin } /${ PageType . About } ` ;
357+
358+ await analyticsClean . page ( PageType . About ) ;
359+
360+ expect ( coreTrackSpy ) . toHaveBeenCalledWith (
361+ expect . objectContaining ( {
362+ type : analyticsTrackTypes . Page ,
363+ event : PageType . About ,
364+ context : expect . objectContaining ( {
365+ web : expect . objectContaining ( {
366+ pageLocationReferrer : `${ origin } /${ PageType . Homepage } ` ,
367+ } ) ,
368+ } ) ,
369+ } ) ,
370+ ) ;
371+ } ) ;
372+
373+ it ( 'should set `pageLocationReferrer` value to the previous page instead of document.referrer on track actions' , async ( ) => {
374+ const origin = 'www.example.com' ;
375+
376+ jest . spyOn ( document , 'referrer' , 'get' ) . mockReturnValueOnce ( origin ) ;
377+ window . location . href = `${ origin } /${ PageType . Homepage } ` ;
378+
379+ const analyticsClean = newAnalyticsIntance ( ) ;
380+
381+ await analyticsClean . track ( mockEvent , mockEventProperties ) ;
382+
383+ expect ( coreTrackSpy ) . toHaveBeenCalledWith (
384+ expect . objectContaining ( {
385+ type : analyticsTrackTypes . Track ,
386+ event : mockEvent ,
387+ context : expect . objectContaining ( {
388+ web : expect . objectContaining ( {
389+ pageLocationReferrer : origin ,
390+ } ) ,
391+ } ) ,
392+ } ) ,
393+ ) ;
394+
395+ await analyticsClean . page ( PageType . Homepage ) ;
396+
397+ expect ( coreTrackSpy ) . toHaveBeenCalledWith (
398+ expect . objectContaining ( {
399+ type : analyticsTrackTypes . Page ,
400+ event : PageType . Homepage ,
401+ context : expect . objectContaining ( {
402+ web : expect . objectContaining ( {
403+ pageLocationReferrer : `${ origin } /${ PageType . Homepage } ` ,
404+ } ) ,
405+ } ) ,
406+ } ) ,
407+ ) ;
408+ } ) ;
409+ } ) ;
285410} ) ;
0 commit comments