From e2c4f5c629b07627c661a36758cfc63c438796c0 Mon Sep 17 00:00:00 2001 From: Owen Buckley Date: Thu, 5 Sep 2019 20:06:02 -0400 Subject: [PATCH 1/8] working example with testse --- packages/plugin-google-analytics/README.md | 37 ++++++ packages/plugin-google-analytics/package.json | 21 ++++ packages/plugin-google-analytics/src/index.js | 20 ++++ .../test/cases/default/default.spec.js | 107 ++++++++++++++++++ .../test/cases/default/greenwood.config.js | 7 ++ .../error-analytics-id/greenwood.config.js | 7 ++ test/smoke-test.js | 14 +-- 7 files changed, 203 insertions(+), 10 deletions(-) create mode 100644 packages/plugin-google-analytics/README.md create mode 100644 packages/plugin-google-analytics/package.json create mode 100644 packages/plugin-google-analytics/src/index.js create mode 100644 packages/plugin-google-analytics/test/cases/default/default.spec.js create mode 100644 packages/plugin-google-analytics/test/cases/default/greenwood.config.js create mode 100644 packages/plugin-google-analytics/test/cases/error-analytics-id/greenwood.config.js diff --git a/packages/plugin-google-analytics/README.md b/packages/plugin-google-analytics/README.md new file mode 100644 index 000000000..0625cad34 --- /dev/null +++ b/packages/plugin-google-analytics/README.md @@ -0,0 +1,37 @@ +# @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('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_. \ No newline at end of file diff --git a/packages/plugin-google-analytics/package.json b/packages/plugin-google-analytics/package.json new file mode 100644 index 000000000..3b868783d --- /dev/null +++ b/packages/plugin-google-analytics/package.json @@ -0,0 +1,21 @@ +{ + "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 ", + "license": "MIT", + "main": "src/index.js", + "files": [ + "src/" + ], + "publishConfig": { + "access": "public" + }, + "peerDependencies": { + "@greenwood/cli": "^0.3.6" + }, + "dependencies": { + + } +} \ No newline at end of file diff --git a/packages/plugin-google-analytics/src/index.js b/packages/plugin-google-analytics/src/index.js new file mode 100644 index 000000000..ff98d1935 --- /dev/null +++ b/packages/plugin-google-analytics/src/index.js @@ -0,0 +1,20 @@ +module.exports = (analyticsId) => { + return [{ + type: 'index', + provider: () => { + return { + hookAnalytics: ` + + + + + + ` + }; + } + }]; +}; \ No newline at end of file diff --git a/packages/plugin-google-analytics/test/cases/default/default.spec.js b/packages/plugin-google-analytics/test/cases/default/default.spec.js new file mode 100644 index 000000000..41a26e839 --- /dev/null +++ b/packages/plugin-google-analytics/test/cases/default/default.spec.js @@ -0,0 +1,107 @@ +/* + * Use Case + * Run Greenwood with Google Analytics composite plugin. + * + * Uaer Result + * Should generate a bare bones Greenwood build with certain plugins injected into index.html. + * + * User Command + * greenwood build + * + * User Config + * { + * plugins: [{ + * type: 'index', + * provider: () => { + * return { + * hookAnalytics: ` + * + * ` + * }; + * } + * }] + * + * } + * + * 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.only('Build Greenwood With: ', async function() { + const LABEL = 'Google Analytics Plugin and Default Workspace'; + 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 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('body eve-app ~ script'); + + inlineScript = Array.prototype.slice.call(scriptTags).filter(script => { + return !script.src; + }); + + }); + + it('should be one inline + - - - ` }; } diff --git a/packages/plugin-google-analytics/test/cases/default/default.spec.js b/packages/plugin-google-analytics/test/cases/default/default.spec.js index bf6a13aff..67685cea9 100644 --- a/packages/plugin-google-analytics/test/cases/default/default.spec.js +++ b/packages/plugin-google-analytics/test/cases/default/default.spec.js @@ -29,6 +29,8 @@ const TestBed = require('../../../../../test/test-bed'); describe('Build Greenwood With: ', async function() { const LABEL = 'Google Analytics Plugin and Default Workspace'; + const analyticsId = 'UA-123456-1'; + let setup; before(async function() { @@ -48,7 +50,7 @@ describe('Build Greenwood With: ', async function() { beforeEach(async function() { const dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); - const scriptTags = dom.window.document.querySelectorAll('body eve-app ~ script'); + const scriptTags = dom.window.document.querySelectorAll('head script'); inlineScript = Array.prototype.slice.call(scriptTags).filter(script => { return !script.src; @@ -62,9 +64,11 @@ describe('Build Greenwood With: ', async function() { it('should have the expected code with users analyicsId', function() { const expectedContent = ` - window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; - ga('create', 'UA-123456', 'auto'); - ga('send', 'pageview'); + window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag('js', new Date()); + + gtag('config', '${analyticsId}'); `; expect(inlineScript[0].textContent).to.contain(expectedContent); @@ -76,10 +80,10 @@ describe('Build Greenwood With: ', async function() { beforeEach(async function() { const dom = await JSDOM.fromFile(path.resolve(this.context.publicDir, 'index.html')); - const scriptTags = dom.window.document.querySelectorAll('body eve-app ~ script'); + const scriptTags = dom.window.document.querySelectorAll('head script'); trackingScript = Array.prototype.slice.call(scriptTags).filter(script => { - return script.src === 'https://www.google-analytics.com/analytics.js'; + return script.src === `https://www.googletagmanager.com/gtag/js?id=${analyticsId}`; }); }); diff --git a/packages/plugin-google-analytics/test/cases/default/greenwood.config.js b/packages/plugin-google-analytics/test/cases/default/greenwood.config.js index 175ed3135..af0d429bd 100644 --- a/packages/plugin-google-analytics/test/cases/default/greenwood.config.js +++ b/packages/plugin-google-analytics/test/cases/default/greenwood.config.js @@ -2,6 +2,6 @@ const googleAnalyticsPlugin = require('../../../src/index'); module.exports = { plugins: [ - ...googleAnalyticsPlugin('UA-123456') + ...googleAnalyticsPlugin('UA-123456-1') ] }; \ No newline at end of file diff --git a/test/smoke-test.js b/test/smoke-test.js index 1c860b4f8..23d08b078 100644 --- a/test/smoke-test.js +++ b/test/smoke-test.js @@ -86,6 +86,12 @@ function defaultIndex(label) { expect(title).to.be.equal('Greenwood App'); }); + it('should have three ` diff --git a/packages/plugin-google-analytics/test/cases/default/default.spec.js b/packages/plugin-google-analytics/test/cases/default/default.spec.js index 67685cea9..9acb75fe2 100644 --- a/packages/plugin-google-analytics/test/cases/default/default.spec.js +++ b/packages/plugin-google-analytics/test/cases/default/default.spec.js @@ -1,6 +1,6 @@ /* * Use Case - * Run Greenwood with Google Analytics composite plugin. + * 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. @@ -13,7 +13,9 @@ * * { * plugins: [{ - * ...googleAnalyticsPlugin() + * ...googleAnalyticsPlugin({ + * analyticsId: 'UA-123456-1' + * }) * }] * * } @@ -28,8 +30,8 @@ const runSmokeTest = require('../../../../../test/smoke-test'); const TestBed = require('../../../../../test/test-bed'); describe('Build Greenwood With: ', async function() { - const LABEL = 'Google Analytics Plugin and Default Workspace'; - const analyticsId = 'UA-123456-1'; + const LABEL = 'Google Analytics Plugin with default options and Default Workspace'; + const mockAnalyticsId = 'UA-123456-1'; let setup; @@ -68,7 +70,8 @@ describe('Build Greenwood With: ', async function() { function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', '${analyticsId}'); + gtag('config', '${mockAnalyticsId}', { 'anonymize_ip': true }); + gtag('config', '${mockAnalyticsId}'); `; expect(inlineScript[0].textContent).to.contain(expectedContent); @@ -83,7 +86,7 @@ describe('Build Greenwood With: ', async function() { 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=${analyticsId}`; + return script.src === `https://www.googletagmanager.com/gtag/js?id=${mockAnalyticsId}`; }); }); diff --git a/packages/plugin-google-analytics/test/cases/default/greenwood.config.js b/packages/plugin-google-analytics/test/cases/default/greenwood.config.js index af0d429bd..9b18283c0 100644 --- a/packages/plugin-google-analytics/test/cases/default/greenwood.config.js +++ b/packages/plugin-google-analytics/test/cases/default/greenwood.config.js @@ -2,6 +2,8 @@ const googleAnalyticsPlugin = require('../../../src/index'); module.exports = { plugins: [ - ...googleAnalyticsPlugin('UA-123456-1') + ...googleAnalyticsPlugin({ + analyticsId: 'UA-123456-1' + }) ] }; \ No newline at end of file diff --git a/packages/plugin-google-analytics/test/cases/option-anonymous/greenwood.config.js b/packages/plugin-google-analytics/test/cases/option-anonymous/greenwood.config.js new file mode 100644 index 000000000..764538e7b --- /dev/null +++ b/packages/plugin-google-analytics/test/cases/option-anonymous/greenwood.config.js @@ -0,0 +1,10 @@ +const googleAnalyticsPlugin = require('../../../src/index'); + +module.exports = { + plugins: [ + ...googleAnalyticsPlugin({ + analyticsId: 'UA-123456-1', + anonymous: false + }) + ] +}; \ No newline at end of file diff --git a/packages/plugin-google-analytics/test/cases/option-anonymous/option-anonymous.spec.js b/packages/plugin-google-analytics/test/cases/option-anonymous/option-anonymous.spec.js new file mode 100644 index 000000000..3c07508a2 --- /dev/null +++ b/packages/plugin-google-analytics/test/cases/option-anonymous/option-anonymous.spec.js @@ -0,0 +1,110 @@ +/* + * Use Case + * Run Greenwood with Google Analytics composite plugin with IP anonymization set to false. + * + * 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', + * anonymouse: false + * }) + * }] + * + * } + * + * 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 IP Anonymization tracking set to false 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 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