Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
wyze committed May 21, 2018
0 parents commit 3365ff0
Show file tree
Hide file tree
Showing 23 changed files with 5,673 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* text=auto
*.ts text eol=lf
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Dependencies
node_modules

# Coverage
coverage

# Logs
*-error.log

# Build
.rpt2_cache
gatsby-*.js
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
sudo: false

language: node_js

node_js:
- 10
- 8

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH="$HOME/.yarn/bin:$PATH"

after_success:
- yarn test --coverage
- bash <(curl -s https://codecov.io/bash) -f coverage/lcov.info

cache:
yarn: true
directories:
- $HOME/.cache
- node_modules
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
## Change Log
9 changes: 9 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) Neil Kistner <neil.kistner@gmail.com> (neilkistner.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
59 changes: 59 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "@wyze/gatsby-plugin-google-analytics",
"version": "0.0.0",
"description": "A Gatsby plugin to add Google Analytics.",
"license": "MIT",
"repository": "wyze/gatsby-plugin-google-analytics",
"author": {
"name": "Neil Kistner",
"email": "neil.kistner@gmail.com",
"url": "neilkistner.com"
},
"scripts": {
"build": "microbundle --sourcemap false --format cjs --output . src/gatsby-*",
"clean": "rimraf .rpt2_cache gatsby-*.js",
"dev": "microbundle watch --sourcemap false --format cjs --output . src/gatsby-*",
"postbuild": "find . -name '*.ts.js' -exec sh -c 'mv \"$0\" \"${0%.ts.js}.js\"' {} \\;",
"prebuild": "yarn clean",
"test": "jest",
"version": "write-changelog"
},
"files": [
"gatsby-browser.js",
"gatsby-ssr.js"
],
"keywords": [
"gatsby",
"gatsby-plugin",
"google analytics"
],
"devDependencies": {
"@types/jest": "^22.2.3",
"@types/node": "^10.1.1",
"jest": "^22.4.3",
"microbundle": "^0.4.4",
"react": "^16.3.2",
"rimraf": "^2.6.2",
"ts-jest": "^22.4.4",
"typescript": "^2.8.3",
"write-changelog": "^1.1.0"
},
"peerDependencies": {
"gatsby": "^1.0.0",
"react": "^15.0.0 || ^16.0.0"
},
"publishConfig": {
"access": "public"
},
"jest": {
"moduleFileExtensions": [
"js",
"ts",
"tsx"
],
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.ts$",
"transform": {
"^.+\\.tsx?$": "ts-jest"
}
}
}
116 changes: 116 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
# @wyze/gatsby-plugin-google-analytics

[![Build Status][travis-image]][travis-url]
[![npm][npm-image]][npm-url]
[![Codecov.io][codecov-image]][codecov-url]

> A Gatsby plugin to add Google Analytics.
## Installation

### Yarn

```
$ yarn add --dev @wyze/gatsby-plugin-google-analytics
```

### npm

```
$ npm install --save-dev @wyze/gatsby-plugin-google-analytics
```

## Usage

```js
// gatsby-config.js

module.exports = {
// ...
plugins: [
{
resolve: '@wyze/gatsby-plugin-google-analytics',
options: {
gaPlugins: {
config: [
{
name: 'eventTracker',
options: {
events: [ 'click', 'contextmenu' ],
hitFilter: [
'@@/ga-function',
[ 'model', 'element', 'event', `model.set('eventAction', event.type, true)` ],
],
},
},
'outboundLinkTracker',
],
sources: 'js/autotrack.custom.js',
}
trackingId: 'UA-123456-1',
},
},
]
}
```

### Options

| Option | Type | Default | Description |
|--------|------|---------|-------------|
| anonymize | boolean | true | Anonymize information sent to GA. |
| gaPlugins | object | | Specify Google Analytic plugins. |
| **gaPlugins.config** | (object \| string)[] | | Configuration for additional scripts that were included. |
| **gaPlugins.config[].name** | string | | Plugin name to require. |
| **gaPlugins.config[].options** | object | | Options to pass to plugin. |
| **gaPlugins.sources** | string[] | | Additional scripts to include. |
| respectDNT | boolean | true | Respect user's `Do Not Track` setting. |
| **trackingId** | string | | Tracking code from Google Analytics. |

*Note:* Bold denotes required fields.

### Function as a gaPlugin option

Some plugins for Google Analytics allow you to specify a function to override certain functionality. Unfortunalty Gatsby calls `JSON.stringify` on plugin options before passing them into the plugin. This plugin uses special syntax to work around that limitation and pass a function to the Google Analytics plugin.

See the example for [`eventTracker`](https://github.com/googleanalytics/autotrack/blob/master/docs/common-options.md#eventtracker-1) and we will replicate the plugin options below.

```js
module.exports = {
// ...
gaPlugins: {
config: [
{
name: 'eventTracker',
options: {
events: [ 'click', 'auxclick', 'contextmenu' ],
// Key here is specifying an array with a special key
hitFilter: [
'@@/ga-function',
[ 'model', 'element', 'event', `model.set('eventAction', event.type, true)` ],
],
},
},
],
},
}
```

In order to transform a key to a function, we specify it as an array. The first item **must** be `@@/ga-function`, with the second item being arguments to the [`Function`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function) constructor. All of the items in the array minus the last item, will be the arguments of the function. The last item in the array will be the function body.

## Change Log

> [Full Change Log](changelog.md)
## License

MIT © [Neil Kistner](//neilkistner.com)

[travis-image]: https://img.shields.io/travis/wyze/gatsby-plugin-google-analytics.svg?style=flat-square
[travis-url]: https://travis-ci.org/wyze/gatsby-plugin-google-analytics

[npm-image]: https://img.shields.io/npm/v/@wyze/gatsby-plugin-google-analytics.svg?style=flat-square
[npm-url]: https://npmjs.com/package/@wyze/gatsby-plugin-google-analytics

[codecov-image]: https://img.shields.io/codecov/c/github/wyze/gatsby-plugin-google-analytics.svg?style=flat-square
[codecov-url]: https://codecov.io/github/wyze/gatsby-plugin-google-analytics
10 changes: 10 additions & 0 deletions src/__tests__/gatsby-browser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as gatsbyBrowser from '../gatsby-browser'

describe('gatsby-browser', () => {
it('exports functions', () => {
expect(gatsbyBrowser).toEqual({
__esModule: true,
onRouteUpdate: expect.any(Function),
})
})
})
10 changes: 10 additions & 0 deletions src/__tests__/gatsby-ssr.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as gatsbySsr from '../gatsby-ssr'

describe('gatsby-ssr', () => {
it('exports functions', () => {
expect(gatsbySsr).toEqual({
__esModule: true,
onRenderBody: expect.any(Function),
})
})
})
105 changes: 105 additions & 0 deletions src/api/__tests__/__snapshots__/on-render-body.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`onRenderBody adds the google analytic scripts 1`] = `
Array [
Array [
<script
dangerouslySetInnerHTML={
Object {
"__html": "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create','1','auto');ga('require','dnt');ga('set','anonymizeIp',1);",
}
}
/>,
<Script
source="//www.google-analytics.com/analytics.js"
/>,
<Script
source="//storage.googleapis.com/outfox/dnt_min.js"
/>,
],
]
`;

exports[`onRenderBody adds the google analytic scripts with plugins 1`] = `
Array [
Array [
<script
dangerouslySetInnerHTML={
Object {
"__html": "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create','1','auto');ga('require','eventTracker');ga('require','dnt');ga('set','anonymizeIp',1);",
}
}
/>,
<Script
source="//www.google-analytics.com/analytics.js"
/>,
<Script
source="//storage.googleapis.com/outfox/dnt_min.js"
/>,
<Script
source="js/autotrack.custom.js"
/>,
],
]
`;

exports[`onRenderBody adds the google analytic scripts with plugins and plugin options 1`] = `
Array [
Array [
<script
dangerouslySetInnerHTML={
Object {
"__html": "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create','1','auto');ga('require','eventTracker',{events:[\\"click\\"],hitFilter:function(model,element,event) {model.set('eventAction', event.type, true)}});ga('require','dnt');ga('set','anonymizeIp',1);",
}
}
/>,
<Script
source="//www.google-analytics.com/analytics.js"
/>,
<Script
source="//storage.googleapis.com/outfox/dnt_min.js"
/>,
<Script
source="js/autotrack.custom.js"
/>,
],
]
`;

exports[`onRenderBody adds the google analytic scripts without anonymize 1`] = `
Array [
Array [
<script
dangerouslySetInnerHTML={
Object {
"__html": "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create','1','auto');ga('require','dnt');",
}
}
/>,
<Script
source="//www.google-analytics.com/analytics.js"
/>,
<Script
source="//storage.googleapis.com/outfox/dnt_min.js"
/>,
],
]
`;

exports[`onRenderBody adds the google analytic scripts without respectDNT 1`] = `
Array [
Array [
<script
dangerouslySetInnerHTML={
Object {
"__html": "window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;ga('create','1','auto');ga('set','anonymizeIp',1);",
}
}
/>,
<Script
source="//www.google-analytics.com/analytics.js"
/>,
false,
],
]
`;
Loading

0 comments on commit 3365ff0

Please sign in to comment.