Skip to content
Permalink
Browse files

initial commit 🍹

  • Loading branch information...
kremalicious committed May 7, 2018
0 parents commit 49bffd3cf18f1ba9e099048f9a5591f9200f3296
Showing with 231 additions and 0 deletions.
  1. +7 βˆ’0 .babelrc
  2. +10 βˆ’0 .editorconfig
  3. +26 βˆ’0 .eslintrc
  4. +3 βˆ’0 .gitignore
  5. +2 βˆ’0 .npmignore
  6. +7 βˆ’0 LICENSE
  7. +52 βˆ’0 README.md
  8. +39 βˆ’0 package.json
  9. +32 βˆ’0 src/gatsby-browser.js
  10. +53 βˆ’0 src/gatsby-ssr.js
@@ -0,0 +1,7 @@
{
"presets": [
["env", { "browser": true }],
["react"],
["flow"]
]
}
@@ -0,0 +1,10 @@

# EditorConfig is awesome: http://EditorConfig.org

[*]
indent_style = space
indent_size = 2
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true
@@ -0,0 +1,26 @@
{
"extends": ["eslint:recommended", "plugin:react/recommended"],
"plugins": ["react"],
"rules": {
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
},
"parserOptions": {
"sourceType": "module",
"ecmaFeatures": {
"jsx": true,
"modules": true
}
},
"env": {
"browser": true,
"node": true,
"es6": true
}
}
@@ -0,0 +1,3 @@
/*.js
node_modules
package-lock.json
@@ -0,0 +1,2 @@
node_modules
src
@@ -0,0 +1,7 @@
Copyright (c) 2018 Matthias Kretschmann m@kretschmann.io

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.
@@ -0,0 +1,52 @@
# gatsby-plugin-matomo

> Gatsby plugin to add Matomo (formerly Piwik) onto a site.
## Usage

1. First, install the plugin from your project's root:

```bash
cd yourproject/
npm i gatsby-plugin-matomo
```

2. Then load the plugin from your `gatsby-config.js` and set the required variables:

```js
plugins: [
{
resolve: 'gatsby-plugin-matomo',
options: {
siteId: 'YOUR_SITE_ID',
siteUrl: 'https://YOUR_LIVE_SITE_URL.COM',
matomoUrl: 'https://YOUR_MATOMO_URL.COM',
},
},
]
```

3. That's it!

## Features

Plugin uses sensible defaults prioritizing user experience & privacy:

- include tracking code in all SSR routes
- track all route views as custom events
- load tracking scripts at end of `body` tag
- use image tracking fallback for `noscript`
- don't load anything when visitor has Do Not Track enabled
- don't load anything in non-production environments

## License

The MIT License

Copyright (c) 2018 Matthias Kretschmann m@kretschmann.io

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.
@@ -0,0 +1,39 @@
{
"name": "gatsby-plugin-matomo",
"description": "Gatsby plugin to add Matomo (formerly Piwik) onto a site.",
"version": "0.0.1",
"author": "Matthias Kretschmann <m@kretschmann.io>",
"bugs": {
"url": "https://github.com/kremalicious/gatsby-plugin-matomo/issues"
},
"dependencies": {
"babel-runtime": "^6.26.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"cross-env": "^5.0.5",
"eslint": "^4.19.1",
"eslint-plugin-react": "^7.7.0"
},
"homepage": "https://github.com/kremalicious/gatsby-plugin-matomo",
"keywords": [
"gatsby",
"gatsby-plugin",
"matomo",
"piwik"
],
"license": "MIT",
"main": "index.js",
"peerDependencies": {
"gatsby": "^1.9.0"
},
"repository": "https://github.com/kremalicious/gatsby-plugin-matomo.git",
"scripts": {
"build": "babel src --out-dir . --ignore __tests__",
"prepublishOnly": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore __tests__",
"format": "prettier --write 'src/**/*.{js,jsx}'"
}
}
@@ -0,0 +1,32 @@
let first = true

function getDuration() {
const start = window.start || new Date()
const now = new Date()
const difference = now.getTime() - start.getTime()

if (difference === 0) {
return null
}

return difference
}

exports.onRouteUpdate = ({ location }) => {
window._paq = window._paq || []

if (first) {
first = false
window._paq.push([
'trackEvent',
'javascript',
'load',
'duration',
getDuration(),
])
} else {
window._paq.push(['setCustomUrl', location.pathname])
window._paq.push(['setDocumentTitle', location.pathname])
window._paq.push(['trackPageView'])
}
}
@@ -0,0 +1,53 @@
import React from 'react'

function buildTrackingCode(siteId, matomoUrl) {
const html = `
if (!(navigator.doNotTrack == '1' || window.doNotTrack == '1')) {
window._paq = window._paq || [];
window._paq.push(['setTrackerUrl', '${matomoUrl}/piwik.php']);
window._paq.push(['setSiteId', '${siteId}']);
window._paq.push(['enableLinkTracking']);
window._paq.push(['trackPageView']);
window._paq.push(['enableHeartBeatTimer']);
window.start = new Date();
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.defer=true; g.async=true;
g.src='${matomoUrl}/piwik.js';
s.parentNode.insertBefore(g,s);
}
`

return (
<script
key={'gatsby-plugin-matomo'}
type="text/javascript"
dangerouslySetInnerHTML={{ __html: html }}
/>
)
}

function buildTrackingCodeNoJs(siteId, matomoUrl, siteUrl, pathname) {
const html = `<img src="${matomoUrl}/piwik.php?idsite=${siteId}&rec=1&url=${siteUrl + pathname}" style="border:0" alt="tracker" />`

return (
<noscript
key={'gatsby-plugin-matomo'}
dangerouslySetInnerHTML={{ __html: html }}
/>
)
}

exports.onRenderBody = ({ setPostBodyComponents, pathname }, pluginOptions) => {
if (process.env.NODE_ENV === 'production') {
const siteId = pluginOptions.siteId
const siteUrl = pluginOptions.siteUrl
const matomoUrl = pluginOptions.matomoUrl

return setPostBodyComponents([
buildTrackingCode(siteId, matomoUrl),
buildTrackingCodeNoJs(siteId, matomoUrl, siteUrl, pathname)
])
}
return null
}

0 comments on commit 49bffd3

Please sign in to comment.
You can’t perform that action at this time.