File size: 891 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
import { loadScript } from '@automattic/load-script';
import { mayWeTrackByTracker } from '../tracker-buckets';
import { PARSLEY_SCRIPT_URL } from './constants';

/**
 * We'll be accessing PARSELY from the window object.
 */
declare global {
	interface Window {
		PARSELY: {
			autotrack: boolean;
			conversions: {
				trackPurchase: ( label: string ) => void;
			};
		};
	}
}

/**
 * Loads the Parsely script, if the user has consented to tracking,
 * and tracking is allowed by the current environment.
 * @returns Promise<void>
 */
export const loadParselyTracker = async (): Promise< void > => {
	// Are we allowed to track (user consent, e2e, etc.)?
	if ( ! mayWeTrackByTracker( 'parsely' ) ) {
		throw new Error( 'Tracking is not allowed' );
	}
	window.PARSELY = { ...window.PARSELY, autotrack: false };
	// Load the Parsely Tracker script
	await loadScript( PARSLEY_SCRIPT_URL );
};