-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
Add google tagmanager plugin #1123
Merged
KyleAMathews
merged 11 commits into
gatsbyjs:1.0
from
0x80:topic/google-tagmanager-plugin
Jun 21, 2017
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
f10d752
Add setPreBodyComponents API
0x80 ed883e0
Add google tagmanager plugin
0x80 f0218af
Fix injected markup and scripts
0x80 e7e7070
Add preBodyComponents to other places
0x80 767ff90
Add todo note
0x80 ff92711
Merge branch '1.0' into topic/google-tagmanager-plugin
0x80 28b19f9
Return a value consistently
0x80 c45fe30
Some cleanup
0x80 61a4d0c
Render iframe with dangerouslySetInnerHTML
0x80 c2871f9
Add some keys to keep linter happy
0x80 5468ce3
Set version to beta.1
0x80 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"presets": [ | ||
["env", { | ||
"targets": { | ||
"browsers": ["last 2 versions", "safari >= 7"] | ||
} | ||
}], | ||
"react" | ||
], | ||
"plugins": [ | ||
"transform-object-rest-spread", | ||
"transform-flow-strip-types" | ||
] | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/*.js | ||
!index.js | ||
yarn.lock |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Logs | ||
logs | ||
*.log | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
|
||
# Directory for instrumented libs generated by jscoverage/JSCover | ||
lib-cov | ||
|
||
# Coverage directory used by tools like istanbul | ||
coverage | ||
|
||
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) | ||
.grunt | ||
|
||
# node-waf configuration | ||
.lock-wscript | ||
|
||
# Compiled binary addons (http://nodejs.org/api/addons.html) | ||
build/Release | ||
|
||
# Dependency directory | ||
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git | ||
node_modules | ||
*.un~ | ||
yarn.lock | ||
src | ||
flow-typed | ||
coverage | ||
decls | ||
examples |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# gatsby-plugin-google-analytics | ||
|
||
Easily add Google Tagmanager to your Gatsby site. | ||
|
||
## Install | ||
`npm install --save gatsby-plugin-google-tagmanager` | ||
|
||
## How to use | ||
|
||
```javascript | ||
// In your gatsby-config.js | ||
plugins: [ | ||
{ | ||
resolve: `gatsby-plugin-google-tagmanager`, | ||
options: { | ||
id: 'YOUR_GOOGLE_TAGMANAGER_ID', | ||
}, | ||
}, | ||
] | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
// noop |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "gatsby-plugin-google-tagmanager", | ||
"description": "Gatsby plugin to add google tagmanager onto a site", | ||
"version": "1.0.0-beta.1", | ||
"author": "Thijs Koerselman <thijs@vauxlab.com>", | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1" | ||
}, | ||
"keywords": [ | ||
"gatsby", | ||
"gatsby-plugin", | ||
"google analytics", | ||
"google tagmanager" | ||
], | ||
"license": "MIT", | ||
"main": "index.js", | ||
"scripts": { | ||
"build": "babel src --out-dir .", | ||
"watch": "babel -w src --out-dir ." | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
packages/gatsby-plugin-google-tagmanager/src/gatsby-ssr.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from "react" | ||
import { stripIndent } from "common-tags" | ||
|
||
exports.onRenderBody = ( | ||
{ setHeadComponents, setPreBodyComponents }, | ||
pluginOptions | ||
) => { | ||
if (process.env.NODE_ENV === `production`) { | ||
setHeadComponents([ | ||
<script | ||
key="plugin-google-tagmanager" | ||
dangerouslySetInnerHTML={{ | ||
__html: stripIndent` | ||
(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', '${pluginOptions.id}');`, | ||
}} | ||
/>, | ||
]) | ||
|
||
setPreBodyComponents([ | ||
<noscript | ||
key="plugin-google-tagmanager" | ||
dangerouslySetInnerHTML={{ | ||
__html: stripIndent` | ||
<iframe | ||
src="https://www.googletagmanager.com/ns.html?id=${pluginOptions.id}" | ||
height="0" | ||
width="0" | ||
style="display: none; visibility: hidden" | ||
/>`, | ||
}} | ||
/>, | ||
]) | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why a separate babelrc? Ideally every plugin uses the central one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mindless copy-paste from the google analytics plugin :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh, Kyle! Why did you include a babelrc there??
At some point we need to just centrally manage all the npmignore, gitignore, etc. files.
You mind removing both then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I just remembered that I put it there to fix a bug! Client side code needs it because otherwise it wont work in older browsers. The backticks will end up in the production bundle for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think ideally we want (especially) these plugins to export code in a way that the user build process can transpile them together with the user code. I was reading this on the subject recently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this light I have some new ideas about plugin package structure as well. Because there are standards to distinguish between browser and node parts of a package, I figured that if we can merge the gatsby-ssr and gatsby-node api's in a single entry (since they are both node context), we can use the same technique as other packages use for making the distinction.
You would basically have a
main
field pointing to the mergedgatsby-ssr
&gatsby-node
entry, and abrowser
field pointing to thegatsby-browser
entry.Then in the future when webpack supports subfields in
esnext
you can haveesnext.browser
point to the raw gatsby-browser code etc. So then plugin packages can export transpiled code and raw code for both contexts at the same time like this:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm yeah you're right. That is what we should do. We do after all control the builds so this would be easy to setup. We'd just ship the source code and then users could configure their browserlist according to the needs of their project https://github.com/ai/browserslist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gatsby-ssr is not a node environment. It's compiled by webpack and can include webpack stuff like importing files etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see.