mirror of
https://github.com/kremalicious/gatsby-plugin-matomo.git
synced 2024-12-22 17:23:23 +01:00
initial commit 🍹
This commit is contained in:
commit
49bffd3cf1
7
.babelrc
Normal file
7
.babelrc
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"presets": [
|
||||||
|
["env", { "browser": true }],
|
||||||
|
["react"],
|
||||||
|
["flow"]
|
||||||
|
]
|
||||||
|
}
|
10
.editorconfig
Normal file
10
.editorconfig
Normal file
@ -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
|
26
.eslintrc
Normal file
26
.eslintrc
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
/*.js
|
||||||
|
node_modules
|
||||||
|
package-lock.json
|
2
.npmignore
Normal file
2
.npmignore
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
node_modules
|
||||||
|
src
|
7
LICENSE
Normal file
7
LICENSE
Normal file
@ -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.
|
52
README.md
Normal file
52
README.md
Normal file
@ -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.
|
39
package.json
Normal file
39
package.json
Normal file
@ -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}'"
|
||||||
|
}
|
||||||
|
}
|
32
src/gatsby-browser.js
Normal file
32
src/gatsby-browser.js
Normal file
@ -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'])
|
||||||
|
}
|
||||||
|
}
|
53
src/gatsby-ssr.js
Normal file
53
src/gatsby-ssr.js
Normal file
@ -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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user