-
Notifications
You must be signed in to change notification settings - Fork 1
fix: google tag manager script #955
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f012c33
fix: google tag manager script
mkilpatrick c03d4b4
robot comments
mkilpatrick b7c6223
more bots
mkilpatrick 3859123
nits
mkilpatrick 670d1e7
Update component screenshots for visual-editor
github-actions[bot] 6ef2950
memo the json parsing
mkilpatrick b87ac1c
Update component screenshots for visual-editor
github-actions[bot] 087b756
more robot
mkilpatrick a7cace0
fix type
mkilpatrick 9cf564a
Alexis nits
mkilpatrick c1ca0d7
Merge branch 'main' into gtm
benlife5 85156f7
update comment
benlife5 4fd4273
fix build
benlife5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| src={`https://www.googletagmanager.com/ns.html?id=${googleTagManagerId}`} | ||
| height="0" | ||
| width="0" | ||
| style={{ display: "none", visibility: "hidden" }} | ||
| /> | ||
| </noscript> | ||
| {/* End Google Tag Manager (noscript) */} | ||
|
mkilpatrick marked this conversation as resolved.
|
||
|
|
||
| {children} | ||
| </> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.