diff --git a/packages/static-site/src/pages/DataLayerGuide.tsx b/packages/static-site/src/pages/DataLayerGuide.tsx index 427d3cfae9..d5ddd8b223 100644 --- a/packages/static-site/src/pages/DataLayerGuide.tsx +++ b/packages/static-site/src/pages/DataLayerGuide.tsx @@ -1,6 +1,15 @@ import { FC } from "react"; import { Link } from "react-router-dom"; import { PagePaths } from "~/routes/config"; +import { default as watchDataLayer } from "~/utils/watchDataLayerBookmarklet?raw"; + +export const bookmarklet = `javascript:(${watchDataLayer.toString() + // remove line breaks + .replace(/\n/g,"") + // remove comments + .replace(/\/\*.*?\*\//g, "") + // replace multiple spaces with a single space + .replace(/\s+/g, " ")})();` const gtmCodes = { universal: "GTM-KDWN8Z", @@ -16,13 +25,13 @@ const GtmCodeExample: FC<{ gtmCode: string }> = ({ gtmCode }) => { */ const headScript = ` - - - `; + + + `; const bodyScript = `
++ Including the following script tag will automatically initialize the GA push events and header scroll + behavior and should be placed in the {``} tag of your HTML. + The script has event listeners to initialize on the window load event. +
{`
-
-
+
`}
- Then, the following code should be placed towards the bottom of your - html doc or deferred. This initializes the data layer and the global - header classes + If for some reason your setup includes the script after the window load event has already occurred, you can call the initialization functions directly, by using the global variable {`unityBootstrap`}.
- {" "}
{`
-
+
`}
- {" "}
+
+
+
+
+
+ Drag this link to your bookmarks bar. When you are on a page with
+ data layer events, clicking the bookmarklet will execute a tiny
+ script which will output data layer events to the console.
+ Watch DataLayer
+
+ As a user, always exercise caution when running code. Only click + or add bookmarklets if at least one of the following applies: +
+
+ {`
+${watchDataLayer.toString()}
+ `}
+
diff --git a/packages/static-site/src/utils/baseUrl.ts b/packages/static-site/src/utils/baseUrl.ts
index b54a4b5636..1059b199fd 100644
--- a/packages/static-site/src/utils/baseUrl.ts
+++ b/packages/static-site/src/utils/baseUrl.ts
@@ -1,11 +1,21 @@
+import { PagePaths } from '../routes/config';
+
export const getBaseUrl = () => {
+ const pages = Object.values(PagePaths)
+ .filter((page) => page !== '/')
+ .map((page) => page.replace(/\//g, ''));
+
if (typeof window === 'undefined') return '/';
const pathname = window.location.pathname;
- const segments = pathname.split('/').filter(Boolean);
- return segments.length > 0 ? `/${segments[0]}` : '/';
+ const segments = pathname.split('/')
+ .filter(Boolean)
+ .filter((segment) => !pages.includes(segment))
+ .filter((segment) => segment.indexOf(".") === -1);
+
+ return `/${segments.join('/')}`;
};
diff --git a/packages/static-site/src/utils/watchDataLayerBookmarklet.js b/packages/static-site/src/utils/watchDataLayerBookmarklet.js
new file mode 100644
index 0000000000..946cd9fed8
--- /dev/null
+++ b/packages/static-site/src/utils/watchDataLayerBookmarklet.js
@@ -0,0 +1,38 @@
+() => {
+ const w = window
+ , dl = w.dataLayer || []
+ /* used to provide styling in the console */
+ , styleKey = '\x1B[38;2;155;145;134;2m'
+ , styleValue = '\x1B[38;2;223;55;48;2m'
+ , styleDefault = '\x1B[0;3m'
+ , styleHeading = '\x1B[0;34;1m'
+ /* provide a global variable */
+ , tableFnName = 'showDataLayerTable';
+ /* display all watched events as console table */
+ w[tableFnName] = () => console.table(dl.slice().filter(i =>
+ typeof i.name !== 'undefined'));
+
+ if (typeof w.onbeforeunload !== 'function') {
+ /* initial click of bookmarklet */
+ dl.push = new Proxy(dl.push,{
+ apply: (target, thisArg, args) => {
+ console.log(`${styleHeading}dataLayer event\n${styleDefault}
+ { ${(Object.entries(args[0]).map( ([k,v]) => (
+ `${styleKey}${k}${styleDefault}: ${styleValue}'${v}'${styleDefault}`
+ ))).sort().join(', ')}${styleDefault} }`);
+ return Reflect.apply(target, thisArg, args);
+ }
+ });
+ alert(`DataLayer events will be displayed in console. Rerun bookmarklet or
+ call ${tableFnName}() from the console to display a table of all events.\n\n
+
+A prompt will warn you before leaving the page, this is normal and will allow
+ you to inspect the dataLayer events, that would have reloaded the page.\n\n
+
+Follow the prompt to reload or leave the page if you are done watching events.`);
+ w.onbeforeunload = (e) => '';
+ } else {
+ /* if already initialized, output recorded events */
+ w[tableFnName]();
+ }
+}