@codecademy/tracking 1.0.36-alpha.fe30759fff.0 → 1.0.36

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ export type GTMSettings = {
3
3
  environment: string;
4
4
  scope: TrackingWindow;
5
5
  optedOutExternalTracking?: boolean;
6
- partytown?: boolean;
6
+ enablePartytown?: boolean;
7
7
  };
8
8
  export declare const OPT_OUT_DATALAYER_VAR = "user_opted_out_external_tracking";
9
- export declare const initializeGTM: ({ scope, environment, optedOutExternalTracking, partytown, }: GTMSettings) => void;
9
+ export declare const initializeGTM: ({ scope, environment, optedOutExternalTracking, enablePartytown, }: GTMSettings) => void;
@@ -3,7 +3,7 @@ export const initializeGTM = _ref => {
3
3
  let scope = _ref.scope,
4
4
  environment = _ref.environment,
5
5
  optedOutExternalTracking = _ref.optedOutExternalTracking,
6
- partytown = _ref.partytown;
6
+ enablePartytown = _ref.enablePartytown;
7
7
  scope.dataLayer ??= [];
8
8
  scope.dataLayer.push({
9
9
  'gtm.start': new Date().getTime(),
@@ -22,13 +22,14 @@ export const initializeGTM = _ref => {
22
22
  }
23
23
  const gtm = document.createElement('script');
24
24
  gtm.src = `https://www.googletagmanager.com/gtm.js?id=GTM-KTLK85W${preview_env}`;
25
- if (partytown) {
25
+ if (enablePartytown) {
26
26
  gtm.type = 'text/partytown';
27
27
  } else {
28
28
  gtm.async = true;
29
29
  }
30
30
  document.getElementsByTagName('head')[0].appendChild(gtm);
31
- if (partytown) {
31
+ if (enablePartytown) {
32
+ // Let Partytown know that a new script has been added.
32
33
  window.dispatchEvent(new CustomEvent('ptupdate'));
33
34
  }
34
35
  };
@@ -17,12 +17,11 @@ export type TrackingIntegrationsSettings = {
17
17
  */
18
18
  oneTrustScript?: string;
19
19
  /**
20
- * Use partytown to load scripts.
21
- * next.config.js experimental: { nextScriptWorkers } must be set to true.
20
+ * Use Partytown to load 3rd party scripts in a worker.
22
21
  */
23
- partytown?: boolean;
22
+ enablePartytown?: boolean;
24
23
  };
25
24
  /**
26
25
  * @see README.md for details and usage.
27
26
  */
28
- export declare const initializeTrackingIntegrations: ({ environment, scope, optedOutExternalTracking, oneTrustScript, partytown, }: TrackingIntegrationsSettings) => Promise<void>;
27
+ export declare const initializeTrackingIntegrations: ({ environment, scope, optedOutExternalTracking, oneTrustScript, enablePartytown, }: TrackingIntegrationsSettings) => Promise<void>;
@@ -1,5 +1,8 @@
1
1
  import { initializeGTM } from './gtm';
2
2
  import { initializeOneTrust } from './onetrust';
3
+ import { initializePartytown } from './partytown';
4
+ let init = false;
5
+
3
6
  /**
4
7
  * @see README.md for details and usage.
5
8
  */
@@ -8,24 +11,28 @@ export const initializeTrackingIntegrations = async _ref => {
8
11
  scope = _ref.scope,
9
12
  optedOutExternalTracking = _ref.optedOutExternalTracking,
10
13
  oneTrustScript = _ref.oneTrustScript,
11
- partytown = _ref.partytown;
12
- if (!partytown) {
13
- // 1. Wait 1000ms to allow any other post-hydration logic to run first
14
+ enablePartytown = _ref.enablePartytown;
15
+ if (init) return; // Prevent multiple initializations
16
+ init = true;
17
+ if (enablePartytown) {
18
+ initializePartytown();
19
+ } else {
20
+ // Wait to allow any other post-hydration logic to run first
14
21
  await new Promise(resolve => setTimeout(resolve, 1000));
15
22
  }
16
23
 
17
- // 2. Load in OneTrust's banner and wait for its `OptanonWrapper` callback
24
+ // Load in OneTrust's banner and wait for its `OptanonWrapper` callback
18
25
  await initializeOneTrust({
19
26
  scope,
20
27
  environment,
21
28
  scriptId: oneTrustScript
22
29
  });
23
30
 
24
- // 3. Load GTM
31
+ // Load GTM
25
32
  initializeGTM({
26
33
  scope,
27
34
  environment,
28
35
  optedOutExternalTracking,
29
- partytown
36
+ enablePartytown
30
37
  });
31
38
  };
@@ -0,0 +1,2 @@
1
+ import { PartytownConfig } from '@builder.io/partytown/integration';
2
+ export declare const partytownConfig: () => PartytownConfig;
@@ -0,0 +1,36 @@
1
+ export const partytownConfig = () => ({
2
+ forward: ['dataLayer.push',
3
+ // for GTM
4
+ 'fbq',
5
+ // for Facebook Pixel
6
+ '_hsq.push' // for Hubspot
7
+ ],
8
+ lib: '/partytown/',
9
+ // path for loading from static asset CMS
10
+ loadScriptsOnMainThread: [/googleads/,
11
+ // Google Ads
12
+ /bing/,
13
+ // Bing UET
14
+ /pepperjam/,
15
+ // Pepperjam
16
+ /snap/,
17
+ // Snap Pixel
18
+ /lightboxcdn/ // Digioh
19
+ ],
20
+ // This function runs in a worker and can't access other vars in this file.
21
+ resolveUrl(url, location, type) {
22
+ // Block GTM from trying to add its own service worker via iframe.
23
+ if (url.hostname === 'www.googletagmanager.com' && type === 'iframe') {
24
+ return new URL('', 'https:.');
25
+ }
26
+
27
+ /*
28
+ * Proxy Facebook Pixel requests to resolve CORS issues
29
+ * see https://partytown.builder.io/facebook-pixel#proxy-requests
30
+ */
31
+ if (url.hostname === 'connect.facebook.net') {
32
+ return new URL(`partytown-fb${url.pathname}${url.search}`, location.origin);
33
+ }
34
+ return url;
35
+ }
36
+ });
@@ -0,0 +1 @@
1
+ export declare function initializePartytown(): void;
@@ -0,0 +1,10 @@
1
+ import { partytownSnippet } from '@builder.io/partytown/integration';
2
+ import { partytownConfig } from './config';
3
+
4
+ // Encapsulate to avoid collision of global vars aliased in minification.
5
+ const encapsulate = js => `(() => {${js}})();`;
6
+ export function initializePartytown() {
7
+ const ptScript = document.createElement('script');
8
+ ptScript.innerHTML = encapsulate(partytownSnippet(partytownConfig()));
9
+ document.head.appendChild(ptScript);
10
+ }
package/package.json CHANGED
@@ -1,8 +1,11 @@
1
1
  {
2
2
  "name": "@codecademy/tracking",
3
3
  "description": "Tracking library for Codecademy",
4
- "version": "1.0.36-alpha.fe30759fff.0",
4
+ "version": "1.0.36",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
+ "dependencies": {
7
+ "@builder.io/partytown": "^0.10.2"
8
+ },
6
9
  "files": [
7
10
  "dist/**"
8
11
  ],
@@ -13,5 +16,5 @@
13
16
  "access": "public"
14
17
  },
15
18
  "repository": "git@github.com:codecademy-engineering/mono.git",
16
- "gitHead": "7e943c428103d08c6c236a389712411ecf3a8c3d"
19
+ "gitHead": "5eef9730521dc255c8d8260a9c25978a20e06e68"
17
20
  }