File size: 1,935 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
// pageView is a wrapper for pageview events across Tracks and GA.

import { recordTracksPageViewWithPageParams } from '@automattic/calypso-analytics';
import { resolveDeviceTypeByViewPort } from '@automattic/viewport';
import { retarget as retargetAdTrackers } from 'calypso/lib/analytics/ad-tracking';
import saveImpactAffiliateClickId from 'calypso/lib/analytics/impact-affiliate';
import { updateQueryParamsTracking } from 'calypso/lib/analytics/sem';
import { refreshCountryCodeCookieGdpr, saveCouponQueryArgument } from 'calypso/lib/analytics/utils';
import { gaRecordPageView } from './ga';
import { processQueue } from './queue';
import { referRecordPageView } from './refer';

export function recordPageView( urlPath, pageTitle, params = {}, options = {} ) {
	// Add delay to avoid stale `_dl` in recorded calypso_page_view event details.
	// `_dl` (browserdocumentlocation) is read from the current URL by external JavaScript.
	setTimeout( () => {
		// Add device type to Tracks page view event.
		params.device_type = resolveDeviceTypeByViewPort();

		// Tracks, Google Analytics, Refer platform.
		recordTracksPageViewWithPageParams( urlPath, params );
		safeGoogleAnalyticsPageView(
			urlPath,
			pageTitle,
			options?.useJetpackGoogleAnalytics,
			options?.useAkismetGoogleAnalytics,
			options?.useA8CForAgenciesGoogleAnalytics
		);
		referRecordPageView();
		saveImpactAffiliateClickId();

		// Retargeting.
		saveCouponQueryArgument();
		updateQueryParamsTracking();
		retargetAdTrackers( urlPath );

		// Process queue.
		processQueue();
	}, 0 );
}

async function safeGoogleAnalyticsPageView(
	urlPath,
	pageTitle,
	useJetpackGoogleAnalytics = false,
	useAkismetGoogleAnalytics = false,
	useA8CForAgenciesGoogleAnalytics = false
) {
	await refreshCountryCodeCookieGdpr();
	gaRecordPageView(
		urlPath,
		pageTitle,
		useJetpackGoogleAnalytics,
		useAkismetGoogleAnalytics,
		useA8CForAgenciesGoogleAnalytics
	);
}