Last active
October 30, 2022 04:17
-
-
Save cjkihl/140978ae71c5e34b3969c93abd7e2f59 to your computer and use it in GitHub Desktop.
NextJS GTM Tag.tsx
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
import Script from "next/script"; | |
import { FC } from "react"; | |
// Your Google Tag Manager Container ID | |
const GTM_ID = "GTM-XXXXXXX"; | |
const Tag: FC = () => { | |
return ( | |
<> | |
<Script id="GTMDataLayer" strategy="afterInteractive"> | |
{` | |
window.dataLayer = window.dataLayer || [] | |
window.dataLayer.push({ | |
'gtm.start': new Date().getTime(), | |
event: 'gtm.js', | |
}) | |
`} | |
</Script> | |
<Script | |
async | |
src={`https://www.googletagmanager.com/gtm.js?id=${GTM_ID}`} | |
strategy="afterInteractive" | |
/> | |
<noscript> | |
<iframe | |
src={`https://www.googletagmanager.com/ns.html?id=${GTM_ID}`} | |
height="0" | |
width="0" | |
style={{ display: "none", visibility: "hidden" }} | |
/> | |
</noscript> | |
</> | |
); | |
}; | |
export default Tag; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment