File size: 2,124 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/**
 * @jest-environment jsdom
 */
import isJetpackCloud from 'calypso/lib/jetpack/is-jetpack-cloud';
import { TRACKING_IDS } from '../ad-tracking/constants';
import { setup, firePageView, Ga4PropertyGtag } from '../ad-tracking/google-analytics-4';

jest.mock( 'calypso/lib/jetpack/is-jetpack-cloud' );

jest.mock( 'calypso/lib/analytics/tracker-buckets', () => ( {
	mayWeTrackByTracker: () => true,
	mayWeInitTracker: () => true,
} ) );

describe( 'Google Analytics 4 implementation', () => {
	beforeEach( () => {
		jest.spyOn( window, 'gtag' ).mockImplementation( () => [] );
	} );

	afterEach( () => {
		jest.clearAllMocks();
	} );

	describe( 'Google Analytics 4 initialization on Jetpack Cloud and WPcom', () => {
		test( 'Fire Jetpack Gtag on Jetpack Cloud', () => {
			// We're on Jetpack Cloud.
			isJetpackCloud.mockReturnValue( true );
			const params = {};

			setup( params );
			expect( window.gtag ).toHaveBeenCalledWith(
				'config',
				TRACKING_IDS.jetpackGoogleGA4Gtag,
				params
			);
			// Should also init WPcom gtag.
			expect( window.gtag ).toHaveBeenCalledWith(
				'config',
				TRACKING_IDS.wpcomGoogleGA4Gtag,
				params
			);
		} );
		test( 'Do not fire Jetpack Gtag on WPcom', () => {
			// We're not on Jetpack cloud.
			isJetpackCloud.mockReturnValue( false );
			const params = {};

			setup( params );
			expect( window.gtag ).toHaveBeenCalledWith(
				'config',
				TRACKING_IDS.wpcomGoogleGA4Gtag,
				params
			);

			expect( window.gtag ).not.toHaveBeenCalledWith(
				'config',
				TRACKING_IDS.jetpackGoogleGA4Gtag,
				params
			);
		} );
	} );

	describe( 'Google Analytics 4 Page View events are recorded on WPcom', () => {
		test( 'firePageView results in calling gtag with the expected params', () => {
			// WPcom.
			isJetpackCloud.mockReturnValue( false );

			const gtagParams = {
				send_to: TRACKING_IDS.wpcomGoogleGA4Gtag,
				page_title: 'home',
				page_location: '/',
			};

			firePageView( gtagParams.page_title, gtagParams.page_location, Ga4PropertyGtag.WPCOM );
			expect( window.gtag ).toHaveBeenCalledWith( 'event', 'page_view', gtagParams );
		} );
	} );
} );