Skip to content

Instantly share code, notes, and snippets.

@cjkihl
Last active October 30, 2022 04:17
Show Gist options
  • Save cjkihl/140978ae71c5e34b3969c93abd7e2f59 to your computer and use it in GitHub Desktop.
Save cjkihl/140978ae71c5e34b3969c93abd7e2f59 to your computer and use it in GitHub Desktop.
NextJS GTM Tag.tsx
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