Skip to content
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

feat: add google-tag-manager, fixes in auth #64

Merged
merged 4 commits into from
Feb 2, 2023
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
10 changes: 0 additions & 10 deletions .changeset/four-suns-float.md

This file was deleted.

6 changes: 6 additions & 0 deletions .changeset/twelve-moons-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@wayofdev/google-tag-manager': patch
'web': patch
---

feat: google-tag-manager support in web application
2 changes: 2 additions & 0 deletions apps/web/src/pages/_document.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Analytics } from '@vercel/analytics/react'
import { GoogleTagManagerNoScript } from '@wayofdev/google-tag-manager/src'
import type { DocumentContext, DocumentInitialProps } from 'next/document'
import Document, { Html, Head, Main, NextScript } from 'next/document'
import { i18n } from '../../next-i18next.config'
Expand Down Expand Up @@ -45,6 +46,7 @@ class MyDocument extends Document {
<NextScript />
</body>
<Analytics />
<GoogleTagManagerNoScript />
</Html>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { gtmId } from '../lib/gtm'
export function GoogleTagManagerNoScript() {
return (
<>
<noscript>
{/* eslint-disable-next-line jsx-a11y/iframe-has-title */}
<iframe
src={`https://www.googletagmanager.com/ns.html?id=${gtmId}`}
height="0"
width="0"
style={{ display: 'none', visibility: 'hidden' }}
/>
</noscript>
</>
)
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import { useRouter } from 'next/router'
import Script from 'next/script'
import { useEffect } from 'react'
import { onRouteChangeComplete, gtmId } from '../lib/gtm'

/**
* https://github.com/vercel/next.js/blob/canary/examples/with-google-tag-manager
*/
export function GoogleTagManagerScript() {
const gtmId = process.env.NEXT_PUBLIC_GTM_ID

const router = useRouter()

useEffect(() => {
const onRouteChangeComplete = (url: string) => {
const dataLayer = (globalThis as unknown as { dataLayer: Record<string, unknown>[] })
.dataLayer
dataLayer?.push({ event: 'pageview', page: url })
}
router.events.on('routeChangeComplete', onRouteChangeComplete)
return () => {
router.events.off('routeChangeComplete', onRouteChangeComplete)
Expand All @@ -23,22 +20,13 @@ export function GoogleTagManagerScript() {

return (
<>
<Script id="google-tag-manager" strategy="afterInteractive">{`
<Script id="gtag-base" strategy="afterInteractive">{`
(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', '${gtmId}');
`}</Script>
<noscript>
{/* eslint-disable-next-line jsx-a11y/iframe-has-title */}
<iframe
src={`https://www.googletagmanager.com/ns.html?id=${gtmId}`}
height="0"
width="0"
style={{ display: 'none', visibility: 'hidden' }}
/>
</noscript>
</>
)
}
1 change: 1 addition & 0 deletions packages/google-tag-manager/src/components/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './GoogleTagManagerScript'
export * from './GoogleTagManagerNoScript'
6 changes: 6 additions & 0 deletions packages/google-tag-manager/src/lib/gtm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const gtmId = process.env.NEXT_PUBLIC_GTM_ID

export const onRouteChangeComplete = (url: string) => {
const dataLayer = (globalThis as unknown as { dataLayer: Record<string, unknown>[] }).dataLayer
dataLayer?.push({ event: 'pageview', page: url })
}