@graphcommerce/googletagmanager 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +41 -0
- package/README.md +19 -0
- package/components/GoogleTagManagerNoScript/index.tsx +14 -0
- package/components/GoogleTagManagerScript/index.tsx +24 -0
- package/components/index.ts +2 -0
- package/hooks/index.ts +1 -0
- package/hooks/useGTMPageViewEvent.ts +29 -0
- package/index.ts +2 -0
- package/package.json +31 -0
- package/tsconfig.json +5 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
|
+
|
|
6
|
+
## [0.2.1](https://github.com/ho-nl/m2-pwa/compare/@graphcommerce/googletagmanager@0.2.0...@graphcommerce/googletagmanager@0.2.1) (2021-09-27)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @graphcommerce/googletagmanager
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# 0.2.0 (2021-09-27)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Bug Fixes
|
|
18
|
+
|
|
19
|
+
* use gtm id directly from process.env ([1682834](https://github.com/ho-nl/m2-pwa/commit/16828342b0f432da5c7051b1b9834fdad1c58ec8))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Features
|
|
23
|
+
|
|
24
|
+
* no gtm id present warning ([94cda8a](https://github.com/ho-nl/m2-pwa/commit/94cda8a30a3ec556d3e5484824fbf349e2e1e342))
|
|
25
|
+
* renamed all packages to use [@graphcommerce](https://github.com/graphcommerce) instead of [@reachdigital](https://github.com/reachdigital) ([491e4ce](https://github.com/ho-nl/m2-pwa/commit/491e4cec9a2686472dac36b79f999257c0811ffe))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
# 0.1.0 (2021-09-22)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
### Bug Fixes
|
|
35
|
+
|
|
36
|
+
* use gtm id directly from process.env ([1682834](https://github.com/ho-nl/m2-pwa/commit/16828342b0f432da5c7051b1b9834fdad1c58ec8))
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
### Features
|
|
40
|
+
|
|
41
|
+
* no gtm id present warning ([94cda8a](https://github.com/ho-nl/m2-pwa/commit/94cda8a30a3ec556d3e5484824fbf349e2e1e342))
|
package/README.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# @graphcommerce/googletagmanager
|
|
2
|
+
|
|
3
|
+
This package makes it easy to add Google Tag Manager to your GraphCommerce
|
|
4
|
+
webshop.
|
|
5
|
+
|
|
6
|
+
### Usage
|
|
7
|
+
|
|
8
|
+
1. Fill `NEXT_PUBLIC_GTM_ID` in your .env file
|
|
9
|
+
|
|
10
|
+
2. add `<GoogleTagManagerScript/>` to your \_app.tsx file, preferably as high as
|
|
11
|
+
you can in the component hierarchy
|
|
12
|
+
|
|
13
|
+
3. add `<GoogleTagManagerNoScript/>` to your \_document.tsx file, in the body
|
|
14
|
+
section above `<Main />`
|
|
15
|
+
|
|
16
|
+
### Hooks
|
|
17
|
+
|
|
18
|
+
`useGTMPageViewEvent` - every time a route history change happens, pageview
|
|
19
|
+
event gets triggered
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import React from 'react'
|
|
2
|
+
|
|
3
|
+
export default function GoogleTagManagerNoScript() {
|
|
4
|
+
return (
|
|
5
|
+
<noscript>
|
|
6
|
+
<iframe
|
|
7
|
+
src={`https://www.googletagmanager.com/ns.html?id=${process.env.NEXT_PUBLIC_GTM_ID}`}
|
|
8
|
+
height='0'
|
|
9
|
+
width='0'
|
|
10
|
+
style={{ display: 'none', visibility: 'hidden' }}
|
|
11
|
+
/>
|
|
12
|
+
</noscript>
|
|
13
|
+
)
|
|
14
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import Script from 'next/script'
|
|
2
|
+
import React from 'react'
|
|
3
|
+
|
|
4
|
+
export default function GoogleTagManagerScript() {
|
|
5
|
+
const id = process.env.NEXT_PUBLIC_GTM_ID
|
|
6
|
+
|
|
7
|
+
if (!id) console.warn('[@graphcommerce/googletagmanager]', 'NEXT_PUBLIC_GTM_ID not found')
|
|
8
|
+
|
|
9
|
+
return (
|
|
10
|
+
<Script
|
|
11
|
+
id={`gtm-${id}`}
|
|
12
|
+
strategy='afterInteractive'
|
|
13
|
+
dangerouslySetInnerHTML={{
|
|
14
|
+
__html: `
|
|
15
|
+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
|
|
16
|
+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
|
|
17
|
+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
|
|
18
|
+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
|
|
19
|
+
})(window,document,'script','dataLayer', '${id}');
|
|
20
|
+
`,
|
|
21
|
+
}}
|
|
22
|
+
/>
|
|
23
|
+
)
|
|
24
|
+
}
|
package/hooks/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as useGTMPageViewEvent } from './useGTMPageViewEvent'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { useRouter } from 'next/router'
|
|
2
|
+
import { useEffect } from 'react'
|
|
3
|
+
|
|
4
|
+
type useGTMPageViewEventProps = {
|
|
5
|
+
variables?: { [key: string]: string | number }
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export default function useGTMPageViewEvent(props: useGTMPageViewEventProps = {}) {
|
|
9
|
+
const { variables } = props
|
|
10
|
+
const router = useRouter()
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const onRouteChangeComplete = (url: string) => {
|
|
14
|
+
const win = window as any
|
|
15
|
+
|
|
16
|
+
win.dataLayer.push({
|
|
17
|
+
event: 'pageview',
|
|
18
|
+
page: url,
|
|
19
|
+
...variables,
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
router.events.on('routeChangeComplete', onRouteChangeComplete)
|
|
24
|
+
|
|
25
|
+
return () => {
|
|
26
|
+
router.events.off('routeChangeComplete', onRouteChangeComplete)
|
|
27
|
+
}
|
|
28
|
+
}, [router.events, variables])
|
|
29
|
+
}
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphcommerce/googletagmanager",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"sideEffects": false,
|
|
5
|
+
"prettier": "@graphcommerce/prettier-config-pwa",
|
|
6
|
+
"browserslist": [
|
|
7
|
+
"extends @graphcommerce/browserslist-config-pwa"
|
|
8
|
+
],
|
|
9
|
+
"eslintConfig": {
|
|
10
|
+
"extends": "@graphcommerce/eslint-config-pwa",
|
|
11
|
+
"parserOptions": {
|
|
12
|
+
"project": "./tsconfig.json"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@graphcommerce/browserslist-config-pwa": "^3.0.1",
|
|
17
|
+
"@graphcommerce/eslint-config-pwa": "^3.0.1",
|
|
18
|
+
"@graphcommerce/prettier-config-pwa": "^3.0.1",
|
|
19
|
+
"@graphcommerce/typescript-config-pwa": "^3.0.1",
|
|
20
|
+
"@playwright/test": "^1.14.1"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@apollo/client": "^3.3.21",
|
|
24
|
+
"@graphcommerce/graphql": "^2.103.1",
|
|
25
|
+
"@graphql-typed-document-node/core": "^3.1.0",
|
|
26
|
+
"clsx": "^1.1.1",
|
|
27
|
+
"next": "^11.1.2",
|
|
28
|
+
"react": "^17.0.2",
|
|
29
|
+
"react-dom": "^17.0.2"
|
|
30
|
+
}
|
|
31
|
+
}
|