Skip to content

Commit ed883e0

Browse files
committed
Add google tagmanager plugin
1 parent f10d752 commit ed883e0

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"presets": [
3+
["env", {
4+
"targets": {
5+
"browsers": ["last 2 versions", "safari >= 7"]
6+
}
7+
}],
8+
"react"
9+
],
10+
"plugins": [
11+
"transform-object-rest-spread",
12+
"transform-flow-strip-types"
13+
]
14+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/gatsby-browser.js
2+
/gatsby-ssr.js
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Logs
2+
logs
3+
*.log
4+
5+
# Runtime data
6+
pids
7+
*.pid
8+
*.seed
9+
10+
# Directory for instrumented libs generated by jscoverage/JSCover
11+
lib-cov
12+
13+
# Coverage directory used by tools like istanbul
14+
coverage
15+
16+
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17+
.grunt
18+
19+
# node-waf configuration
20+
.lock-wscript
21+
22+
# Compiled binary addons (http://nodejs.org/api/addons.html)
23+
build/Release
24+
25+
# Dependency directory
26+
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27+
node_modules
28+
*.un~
29+
yarn.lock
30+
src
31+
flow-typed
32+
coverage
33+
decls
34+
examples
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# gatsby-plugin-google-analytics
2+
3+
Easily add Google Tagmanager to your Gatsby site.
4+
5+
## Install
6+
`npm install --save gatsby-plugin-google-tagmanager`
7+
8+
## How to use
9+
10+
```javascript
11+
// In your gatsby-config.js
12+
plugins: [
13+
{
14+
resolve: `gatsby-plugin-google-tagmanager`,
15+
options: {
16+
id: 'YOUR_GOOGLE_TAGMANAGER_ID',
17+
},
18+
},
19+
]
20+
```
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// noop
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "gatsby-plugin-google-tagmanager",
3+
"description": "Gatsby plugin to add google tagmanager onto a site",
4+
"version": "1.0.0-alpha19",
5+
"author": "Thijs Koerselman <[email protected]>",
6+
"devDependencies": {
7+
"babel-cli": "^6.24.1"
8+
},
9+
"keywords": [
10+
"gatsby",
11+
"gatsby-plugin",
12+
"google analytics",
13+
"google tagmanager"
14+
],
15+
"license": "MIT",
16+
"main": "index.js",
17+
"scripts": {
18+
"build": "babel src --out-dir .",
19+
"watch": "babel -w src --out-dir ."
20+
}
21+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from "react"
2+
import { stripIndent } from "common-tags"
3+
4+
exports.onRenderBody = (
5+
{ setHeadComponents, setPreBodyComponents },
6+
pluginOptions
7+
) => {
8+
if (process.env.NODE_ENV === `production`) {
9+
setHeadComponents([
10+
<script
11+
dangerouslySetInnerHTML={{
12+
__html: stripIndent`
13+
<!-- Google Tag Manager -->
14+
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
15+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
16+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
17+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
18+
})(window,document,'script','dataLayer', ${pluginOptions.id});</script>
19+
<!-- End Google Tag Manager -->`,
20+
}}
21+
/>,
22+
])
23+
24+
setPreBodyComponents([
25+
<script
26+
dangerouslySetInnerHTML={{
27+
__html: stripIndent`
28+
<!-- Google Tag Manager (noscript) -->
29+
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=${pluginOptions.id}"
30+
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
31+
<!-- End Google Tag Manager (noscript) -->
32+
`,
33+
}}
34+
/>,
35+
])
36+
}
37+
}

0 commit comments

Comments
 (0)