@codecademy/tracking 1.0.36-alpha.4c322d2d26.0 → 1.0.36-alpha.8939ef2b6b.0

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.
@@ -1,5 +1,6 @@
1
1
  import { initializeGTM } from './gtm';
2
2
  import { initializeOneTrust } from './onetrust';
3
+ import { initializePartytown } from './partytown';
3
4
  /**
4
5
  * @see README.md for details and usage.
5
6
  */
@@ -9,10 +10,8 @@ export const initializeTrackingIntegrations = async _ref => {
9
10
  optedOutExternalTracking = _ref.optedOutExternalTracking,
10
11
  oneTrustScript = _ref.oneTrustScript,
11
12
  partytown = _ref.partytown;
12
- if (!partytown) {
13
- // 1. Wait 1000ms to allow any other post-hydration logic to run first
14
- await new Promise(resolve => setTimeout(resolve, 1000));
15
- }
13
+ // 1. Wait to allow any other post-hydration logic to run first (not as critical when using partytown)
14
+ await new Promise(resolve => setTimeout(resolve, partytown ? 100 : 1000));
16
15
 
17
16
  // 2. Load in OneTrust's banner and wait for its `OptanonWrapper` callback
18
17
  await initializeOneTrust({
@@ -28,4 +27,9 @@ export const initializeTrackingIntegrations = async _ref => {
28
27
  optedOutExternalTracking,
29
28
  partytown
30
29
  });
30
+
31
+ // 4. Load Partytown
32
+ if (partytown) {
33
+ initializePartytown();
34
+ }
31
35
  };
@@ -0,0 +1 @@
1
+ export declare const initializePartytown: () => void;
@@ -0,0 +1,29 @@
1
+ import { partytownSnippet } from '@builder.io/partytown/integration';
2
+ const ptConfig = () => ({
3
+ forward: ['dataLayer.push', 'fbq'],
4
+ lib: '/partytown/',
5
+ loadScriptsOnMainThread: [/googleads/, /bing/, /pepperjam/, /facebook/, /snap/, /lightboxcdn/],
6
+ resolveUrl(url) {
7
+ /*
8
+ * When gtm.js tries to create an iframe for sw_iframe, typically this does
9
+ * not worker becuase it's a cross origin request and google does not include
10
+ * a cross origin response header. As a workaround, we can change the url so
11
+ * it points to a proxy that we have setup in next.config.js. This way, it's
12
+ * on the same origin, and no response cors header is needed.
13
+ */
14
+ const ptGtmProxyBase = `${process.env.NEXT_PUBLIC_BASE_PATH}/partytown-gtm-proxy`;
15
+ if (url.href.includes('sw_iframe')) {
16
+ return new URL(`${ptGtmProxyBase}${url.pathname}`);
17
+ }
18
+ return url;
19
+ }
20
+ });
21
+ const encapsulate = js => `(() => {${js}})();`;
22
+ export const initializePartytown = () => {
23
+ if (!document.getElementById('partytown-init')) {
24
+ const ptScript = document.createElement('script');
25
+ ptScript.id = 'partytown-init';
26
+ ptScript.innerHTML = encapsulate(partytownSnippet(ptConfig()));
27
+ document.head.appendChild(ptScript);
28
+ }
29
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@codecademy/tracking",
3
3
  "description": "Tracking library for Codecademy",
4
- "version": "1.0.36-alpha.4c322d2d26.0",
4
+ "version": "1.0.36-alpha.8939ef2b6b.0",
5
5
  "author": "Codecademy Engineering <dev@codecademy.com>",
6
6
  "files": [
7
7
  "dist/**"
@@ -13,5 +13,5 @@
13
13
  "access": "public"
14
14
  },
15
15
  "repository": "git@github.com:codecademy-engineering/mono.git",
16
- "gitHead": "27ceaa54622a017fa1b01f4098916588edcedcb2"
16
+ "gitHead": "a0cdc42894af72d49e7edcd13ecdbed93c6f8805"
17
17
  }