Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions packages/visual-editor/src/components/GTMBody.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React, { useMemo } from "react";
import { useDocument } from "../hooks/useDocument.tsx";

const GTM_ID_REGEX = /^GTM-[A-Z0-9]+$/;

/**
* Adds the Google Tag Manager (noscript) iframe to the body.
* This is required for GTM to function properly when JavaScript is disabled.
*/
export const GTMBody: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const streamDocument = useDocument();

const visualEditorConfig: Record<string, any> | null = useMemo(() => {
if (!streamDocument?.__?.visualEditorConfig) {
return null;
}

try {
return JSON.parse(streamDocument.__.visualEditorConfig);
} catch (_) {
console.warn(
"Failed to parse visualEditorConfig for GTM. Skipping adding GTM iframe."
);
return null;
}
}, [streamDocument]);

const googleTagManagerId = visualEditorConfig?.googleTagManagerId;

if (!googleTagManagerId || !GTM_ID_REGEX.test(googleTagManagerId)) {
return <>{children}</>;
}

return (
<>
{/* Google Tag Manager (noscript) */}
<noscript>
<iframe
Comment thread
mkilpatrick marked this conversation as resolved.
src={`https://www.googletagmanager.com/ns.html?id=${googleTagManagerId}`}
height="0"
width="0"
style={{ display: "none", visibility: "hidden" }}
/>
</noscript>
{/* End Google Tag Manager (noscript) */}
Comment thread
mkilpatrick marked this conversation as resolved.

{children}
</>
);
};
1 change: 1 addition & 0 deletions packages/visual-editor/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ export {
defaultThemeConfig,
createDefaultThemeConfig,
} from "./DefaultThemeConfig.ts";
export { GTMBody } from "./GTMBody.tsx";
20 changes: 12 additions & 8 deletions packages/visual-editor/src/docs/hybrid-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,24 @@ const Location: Template<TemplateRenderProps> = (props) => {
currency="USD"
>
<VisualEditorProvider templateProps={props}>
<Render
config={mainConfig}
data={migrate(
JSON.parse(document.__.layout),
migrationRegistry,
mainConfig
)}
/>
<GTMBody>
<Render
config={mainConfig}
data={migrate(
JSON.parse(document.__.layout),
migrationRegistry,
mainConfig
)}
/>
</GTMBody>
</VisualEditorProvider>
</AnalyticsProvider>
);
};
```

`<GTMBody>` applies the Google Tag Manager noscript tag if the GTM ID is set in the platform's Site Configuration setting.

`<Render>` converts the saved layout data from the editor into the rendered page.

`migrate` handles updates to the components exported from `@yext/visual-editor`. If you are not using `@yext/visual-editor` components, you do not need to use `migrate`.
Expand Down
37 changes: 31 additions & 6 deletions packages/visual-editor/src/utils/applyAnalytics.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
const GTM_ID_REGEX = /^GTM-[A-Z0-9]+$/;
const GA4_ID_REGEX = /^G(T)?-[A-Z0-9]+$/;

export const applyAnalytics = (document: Record<string, any>) => {
if (!document?.__?.visualEditorConfig) {
return;
}

const googleTagManagerId: string = JSON.parse(
document.__.visualEditorConfig
)?.googleTagManagerId;
let visualEditorConfig: Record<string, any>;
try {
visualEditorConfig = JSON.parse(document.__.visualEditorConfig);
} catch (_) {
console.warn(
"Failed to parse visualEditorConfig for analytics. Skipping analytics script injection."
);
return;
}

// Google Tag Manager (GTM)
const googleTagManagerId = visualEditorConfig?.googleTagManagerId;

if (googleTagManagerId && GTM_ID_REGEX.test(googleTagManagerId)) {
return `<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','${googleTagManagerId}');</script>
<!-- End Google Tag Manager -->`;
}

// Google Analytics 4 (GA4)
const googleAnalyticsId = visualEditorConfig?.googleAnalyticsId;

if (googleTagManagerId) {
if (googleAnalyticsId && GA4_ID_REGEX.test(googleAnalyticsId)) {
return `<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=${googleTagManagerId}"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=${googleAnalyticsId}"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', '${googleTagManagerId}');
gtag('config', '${googleAnalyticsId}');
</script>`;
}
};
Expand Down
13 changes: 8 additions & 5 deletions packages/visual-editor/src/vite-plugin/templates/directory.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getSchema,
injectTranslations,
getCanonicalUrl,
GTMBody,
resolveUrlTemplate,
} from "@yext/visual-editor";
import { AnalyticsProvider, SchemaWrapper } from "@yext/pages-components";
Expand Down Expand Up @@ -148,11 +149,13 @@ const Directory: Template<TemplateRenderProps> = (props) => {
requireOptIn={requireAnalyticsOptIn}
>
<VisualEditorProvider templateProps={props}>
<Render
config={directoryConfig}
data={data}
metadata={{ streamDocument: document }}
/>
<GTMBody>
Comment thread
mkilpatrick marked this conversation as resolved.
<Render
config={directoryConfig}
data={data}
metadata={{ streamDocument: document }}
/>
</GTMBody>
</VisualEditorProvider>
</AnalyticsProvider>
);
Expand Down
13 changes: 8 additions & 5 deletions packages/visual-editor/src/vite-plugin/templates/locator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getCanonicalUrl,
migrate,
migrationRegistry,
GTMBody,
resolveUrlTemplate,
} from "@yext/visual-editor";
import { AnalyticsProvider, SchemaWrapper } from "@yext/pages-components";
Expand Down Expand Up @@ -160,11 +161,13 @@ const Locator: Template<TemplateRenderProps> = (props) => {
requireOptIn={requireAnalyticsOptIn}
>
<VisualEditorProvider templateProps={props}>
<Render
config={locatorConfig}
data={data}
metadata={{ streamDocument: document }}
/>
<GTMBody>
<Render
config={locatorConfig}
data={data}
metadata={{ streamDocument: document }}
/>
</GTMBody>
</VisualEditorProvider>
</AnalyticsProvider>
</>
Expand Down
13 changes: 8 additions & 5 deletions packages/visual-editor/src/vite-plugin/templates/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
getSchema,
injectTranslations,
getCanonicalUrl,
GTMBody,
resolveUrlTemplate,
} from "@yext/visual-editor";
import { AnalyticsProvider, SchemaWrapper } from "@yext/pages-components";
Expand Down Expand Up @@ -158,11 +159,13 @@ const Location: Template<TemplateRenderProps> = (props) => {
requireOptIn={requireAnalyticsOptIn}
>
<VisualEditorProvider templateProps={props}>
<Render
config={filteredConfig}
data={data}
metadata={{ streamDocument: document }}
/>
<GTMBody>
<Render
config={filteredConfig}
data={data}
metadata={{ streamDocument: document }}
/>
</GTMBody>
</VisualEditorProvider>
</AnalyticsProvider>
);
Expand Down
Loading
Loading