commit 49bffd3cf18f1ba9e099048f9a5591f9200f3296 Author: Matthias Kretschmann Date: Mon May 7 18:29:27 2018 +0200 initial commit 🍹 diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..17ad2c4 --- /dev/null +++ b/.babelrc @@ -0,0 +1,7 @@ +{ + "presets": [ + ["env", { "browser": true }], + ["react"], + ["flow"] + ] +} \ No newline at end of file diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..7e07638 --- /dev/null +++ b/.editorconfig @@ -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 \ No newline at end of file diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 0000000..1b01800 --- /dev/null +++ b/.eslintrc @@ -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 + } +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ed93680 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/*.js +node_modules +package-lock.json diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..cd3ca40 --- /dev/null +++ b/.npmignore @@ -0,0 +1,2 @@ +node_modules +src diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..abee374 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..4e3b37c --- /dev/null +++ b/README.md @@ -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. diff --git a/package.json b/package.json new file mode 100644 index 0000000..ab2b12a --- /dev/null +++ b/package.json @@ -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 ", + "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}'" + } +} diff --git a/src/gatsby-browser.js b/src/gatsby-browser.js new file mode 100644 index 0000000..8a009f8 --- /dev/null +++ b/src/gatsby-browser.js @@ -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']) + } +} diff --git a/src/gatsby-ssr.js b/src/gatsby-ssr.js new file mode 100644 index 0000000..9a3ac46 --- /dev/null +++ b/src/gatsby-ssr.js @@ -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 ( +