-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enhancement/issue 186 google analytics plugin (#190)
* working example with testse * error handling * update GA script and specs and integrated into the website * custom plugins page * finish docs * clean up package.json * anonymous IP tracking * add Google SEO meta tag
- Loading branch information
1 parent
11800e7
commit 770e940
Showing
16 changed files
with
429 additions
and
9 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# @greenwood/plugin-google-analytics | ||
|
||
## Overview | ||
A composite plugin for Greenwood for adding support for [Google Analytics](https://developers.google.com/analytics/) JavaScript tracker. For more information and complete docs about Greenwood, please visit the [Greenwood website](https://www.greenwoodjs.io/docs). | ||
|
||
> This package assumes you already have `@greenwood/cli` installed. | ||
## Installation | ||
You can use your favorite JavaScript package manager to install this package. | ||
|
||
_examples:_ | ||
```bash | ||
# npm | ||
npm -i @greenwood/plugin-google-analytics --save-dev | ||
|
||
# yarn | ||
yarn add @greenwood/plugin-google-analytics --dev | ||
``` | ||
|
||
## Usage | ||
Use this plugin in your _greenwood.config.js_ and simply pass in your Google Analytics ID, e.g. `UA-XXXXX`. | ||
|
||
> As this is a composite plugin, you will need to spread the result. | ||
```javascript | ||
const googleAnalyticsPlugin = require('@greenwood/plugin-google-analytics'); | ||
|
||
module.exports = { | ||
... | ||
|
||
plugins: [ | ||
...googleAnalyticsPlugin({ | ||
analyticsId: 'UA-XXXXXX' | ||
}) | ||
] | ||
} | ||
``` | ||
|
||
This will then add the Google Analytics [JavaScript tracker snippet](https://developers.google.com/analytics/devguides/collection/analyticsjs/) to your project's _index.html_. | ||
|
||
### Options | ||
- `analyticsId` (required) - Your Google Analytics ID | ||
- `anonymous` (optional) - If tracking of IPs should be done anonymously. Defaults to `true` |
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,18 @@ | ||
{ | ||
"name": "@greenwood/plugin-google-analytics", | ||
"version": "0.3.6", | ||
"description": "A composite plugin for Greenwood for adding support for Google Analytics.", | ||
"repository": "https://github.com/ProjectEvergreen/greenwood/tree/master/packages/plugin-google-analytics", | ||
"author": "Owen Buckley <[email protected]>", | ||
"license": "MIT", | ||
"main": "src/index.js", | ||
"files": [ | ||
"src/" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"peerDependencies": { | ||
"@greenwood/cli": "^0.3.6" | ||
} | ||
} |
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,30 @@ | ||
module.exports = (options = {}) => { | ||
const { analyticsId, anonymous } = options; | ||
|
||
const validId = analyticsId && typeof analyticsId === 'string'; | ||
const trackAnon = typeof anonymous === 'boolean' ? anonymous : true; | ||
|
||
if (!validId) { | ||
throw new Error(`Error: analyticsId should be of type string. get "${typeof analyticsId}" instead.`); | ||
} | ||
|
||
return [{ | ||
type: 'index', | ||
provider: () => { | ||
return { | ||
hookAnalytics: ` | ||
<script async src="https://www.googletagmanager.com/gtag/js?id=${analyticsId}"></script> | ||
<script> | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${analyticsId}', { 'anonymize_ip': ${trackAnon} }); | ||
gtag('config', '${analyticsId}'); | ||
</script> | ||
` | ||
}; | ||
} | ||
}]; | ||
}; |
109 changes: 109 additions & 0 deletions
109
packages/plugin-google-analytics/test/cases/default/default.spec.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,109 @@ | ||
/* | ||
* Use Case | ||
* Run Greenwood with Google Analytics composite plugin with default options. | ||
* | ||
* Uaer Result | ||
* Should generate a bare bones Greenwood build with certain plugins injected into index.html. | ||
* | ||
* User Command | ||
* greenwood build | ||
* | ||
* User Config | ||
* const googleAnalyticsPlugin = require('@greenwod/plugin-google-analytics'); | ||
* | ||
* { | ||
* plugins: [{ | ||
* ...googleAnalyticsPlugin({ | ||
* analyticsId: 'UA-123456-1' | ||
* }) | ||
* }] | ||
* | ||
* } | ||
* | ||
* User Workspace | ||
* Greenwood default (src/) | ||
*/ | ||
const expect = require('chai').expect; | ||
const { JSDOM } = require('jsdom'); | ||
const path = require('path'); | ||
const runSmokeTest = require('../../../../../test/smoke-test'); | ||
const TestBed = require('../../../../../test/test-bed'); | ||
|
||
describe('Build Greenwood With: ', async function() { | ||
const LABEL = 'Google Analytics Plugin with default options and Default Workspace'; | ||
const mockAnalyticsId = 'UA-123456-1'; | ||
|
||
let setup; | ||
|
||
before(async function() { | ||
setup = new TestBed(); | ||
this.context = setup.setupTestBed(__dirname); | ||
}); | ||
|
||
describe(LABEL, function() { | ||
before(async function() { | ||
await setup.runGreenwoodCommand('build'); | ||
}); | ||
|
||
runSmokeTest(['public', 'index', 'not-found', 'hello'], LABEL); | ||
|
||
describe('Initialization script at the end of the <body> tag', function() { | ||
let inlineScript; | ||
|
||
beforeEach(async function() { | ||
const dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); | ||
const scriptTags = dom.window.document.querySelectorAll('head script'); | ||
|
||
inlineScript = Array.prototype.slice.call(scriptTags).filter(script => { | ||
return !script.src; | ||
}); | ||
|
||
}); | ||
|
||
it('should be one inline <script> tag', function() { | ||
expect(inlineScript.length).to.be.equal(1); | ||
}); | ||
|
||
it('should have the expected code with users analyicsId', function() { | ||
const expectedContent = ` | ||
window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${mockAnalyticsId}', { 'anonymize_ip': true }); | ||
gtag('config', '${mockAnalyticsId}'); | ||
`; | ||
|
||
expect(inlineScript[0].textContent).to.contain(expectedContent); | ||
}); | ||
}); | ||
|
||
describe('Tracking script at the end of the <body> tag', function() { | ||
let trackingScript; | ||
|
||
beforeEach(async function() { | ||
const dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); | ||
const scriptTags = dom.window.document.querySelectorAll('head script'); | ||
|
||
trackingScript = Array.prototype.slice.call(scriptTags).filter(script => { | ||
return script.src === `https://www.googletagmanager.com/gtag/js?id=${mockAnalyticsId}`; | ||
}); | ||
}); | ||
|
||
it('should have one <script> tag for loading the Google Analytics tracker', function() { | ||
expect(trackingScript.length).to.be.equal(1); | ||
}); | ||
|
||
it('should be an async <script> tag for loading the Google Analytics tracker', function() { | ||
const isAsync = trackingScript[0].async !== null; | ||
|
||
expect(isAsync).to.be.equal(true); | ||
}); | ||
}); | ||
}); | ||
|
||
after(function() { | ||
setup.teardownTestBed(); | ||
}); | ||
|
||
}); |
9 changes: 9 additions & 0 deletions
9
packages/plugin-google-analytics/test/cases/default/greenwood.config.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,9 @@ | ||
const googleAnalyticsPlugin = require('../../../src/index'); | ||
|
||
module.exports = { | ||
plugins: [ | ||
...googleAnalyticsPlugin({ | ||
analyticsId: 'UA-123456-1' | ||
}) | ||
] | ||
}; |
49 changes: 49 additions & 0 deletions
49
packages/plugin-google-analytics/test/cases/error-analytics-id/error-analytics-id.spec.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,49 @@ | ||
/* | ||
* Use Case | ||
* Run Greenwood with Google Analytics composite plugin. | ||
* | ||
* Uaer Result | ||
* Should generate an error when not passing in a valid analyticsId. | ||
* | ||
* User Command | ||
* greenwood build | ||
* | ||
* User Config | ||
* const googleAnalyticsPlugin = require('@greenwod/plugin-google-analytics'); | ||
* | ||
* { | ||
* plugins: [{ | ||
* ...googleAnalyticsPlugin() | ||
* }] | ||
* | ||
* } | ||
* | ||
* User Workspace | ||
* Greenwood default (src/) | ||
*/ | ||
const expect = require('chai').expect; | ||
const TestBed = require('../../../../../test/test-bed'); | ||
|
||
describe('Build Greenwood With: ', async function() { | ||
let setup; | ||
|
||
before(async function() { | ||
setup = new TestBed(); | ||
this.context = setup.setupTestBed(__dirname); | ||
}); | ||
|
||
describe('Google Analytics Plugin with a bad value for analyticsId', () => { | ||
it('should throw an error that analyticsId must be a string', async () => { | ||
try { | ||
await setup.runGreenwoodCommand('build'); | ||
} catch (err) { | ||
expect(err).to.contain('analyticsId should be of type string. get "undefined" instead.'); | ||
} | ||
}); | ||
}); | ||
|
||
after(function() { | ||
setup.teardownTestBed(); | ||
}); | ||
|
||
}); |
7 changes: 7 additions & 0 deletions
7
packages/plugin-google-analytics/test/cases/error-analytics-id/greenwood.config.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,7 @@ | ||
const googleAnalyticsPlugin = require('../../../src/index'); | ||
|
||
module.exports = { | ||
plugins: [ | ||
...googleAnalyticsPlugin() | ||
] | ||
}; |
10 changes: 10 additions & 0 deletions
10
packages/plugin-google-analytics/test/cases/option-anonymous/greenwood.config.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,10 @@ | ||
const googleAnalyticsPlugin = require('../../../src/index'); | ||
|
||
module.exports = { | ||
plugins: [ | ||
...googleAnalyticsPlugin({ | ||
analyticsId: 'UA-123456-1', | ||
anonymous: false | ||
}) | ||
] | ||
}; |
Oops, something went wrong.