mirror of
https://github.com/kremalicious/gatsby-plugin-matomo.git
synced 2024-12-22 17:23:23 +01:00
commit
89da8bdc05
16
.eslintrc
16
.eslintrc
@ -1,16 +1,10 @@
|
||||
{
|
||||
"extends": ["eslint:recommended", "plugin:react/recommended"],
|
||||
"plugins": ["react"],
|
||||
"rules": {
|
||||
"quotes": [
|
||||
"error",
|
||||
"single"
|
||||
"extends": [
|
||||
"eslint:recommended",
|
||||
"plugin:react/recommended",
|
||||
"plugin:prettier/recommended"
|
||||
],
|
||||
"semi": [
|
||||
"error",
|
||||
"never"
|
||||
]
|
||||
},
|
||||
"plugins": ["react", "prettier"],
|
||||
"parserOptions": {
|
||||
"sourceType": "module",
|
||||
"ecmaFeatures": {
|
||||
|
5
.prettierrc
Normal file
5
.prettierrc
Normal file
@ -0,0 +1,5 @@
|
||||
{
|
||||
"semi": false,
|
||||
"singleQuote": true,
|
||||
"trailingComma": "none"
|
||||
}
|
@ -1,5 +1,19 @@
|
||||
{
|
||||
"beforeChangelogCommand": "./node_modules/.bin/conventional-changelog -i CHANGELOG.md -s",
|
||||
"changelogCommand": "./node_modules/.bin/conventional-changelog | tail -n +3",
|
||||
"safeBump": false
|
||||
"safeBump": false,
|
||||
"scripts": {
|
||||
"beforeStart": "npm test",
|
||||
"beforeStage": "npm run changelog",
|
||||
"afterRelease": "echo Successfully released ${name} v${version} to ${repo.repository}."
|
||||
},
|
||||
"git": {
|
||||
"tagName": "v${version}"
|
||||
},
|
||||
"github": {
|
||||
"release": true
|
||||
},
|
||||
"npm": {
|
||||
"publish": true
|
||||
}
|
||||
}
|
||||
|
24
README.md
24
README.md
@ -63,16 +63,16 @@ _NOTE: By default, this plugin only generates output when run in production mode
|
||||
|
||||
### Options
|
||||
|
||||
Option | Explanation
|
||||
-----------------|---------
|
||||
`siteId` | Your Matomo site ID configured in your Matomo installation.
|
||||
`matomoUrl` | The url of your Matomo installation.
|
||||
`siteUrl` | The url of your site, usually the same as `siteMetadata.siteUrl`. Only used for generating the url for `noscript` image tracking fallback.
|
||||
`exclude` | (optional) Specify an array of pathnames where tracking code will be excluded. The pathname `/offline-plugin-app-shell-fallback/` is excluded by default.
|
||||
`requireConsent` | (optional) If true, tracking will be disabled until you call `window._paq.push(['setConsentGiven']);`.
|
||||
`disableCookies` | (optional) If true, no cookie will be used by Matomo.
|
||||
`localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`.
|
||||
`dev` | (optional) Activate dev mode by setting to `true`. Will load all scripts despite not running in `production` environment. Ignores your local browser's DNT header too. Outputs some information in console about what it is doing. Useful for local testing but careful: all hits will be send like in production.
|
||||
| Option | Explanation |
|
||||
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
|
||||
| `siteId` | Your Matomo site ID configured in your Matomo installation. |
|
||||
| `matomoUrl` | The url of your Matomo installation. |
|
||||
| `siteUrl` | The url of your site, usually the same as `siteMetadata.siteUrl`. Only used for generating the url for `noscript` image tracking fallback. |
|
||||
| `exclude` | (optional) Specify an array of pathnames where tracking code will be excluded. The pathname `/offline-plugin-app-shell-fallback/` is excluded by default. |
|
||||
| `requireConsent` | (optional) If true, tracking will be disabled until you call `window._paq.push(['setConsentGiven']);`. |
|
||||
| `disableCookies` | (optional) If true, no cookie will be used by Matomo. |
|
||||
| `localScript` | (optional) If set, load local `piwik.js` script from the given path, instead of loading it from your `matomoUrl`. |
|
||||
| `dev` | (optional) Activate dev mode by setting to `true`. Will load all scripts despite not running in `production` environment. Ignores your local browser's DNT header too. Outputs some information in console about what it is doing. Useful for local testing but careful: all hits will be send like in production. |
|
||||
|
||||
```js
|
||||
plugins: [
|
||||
@ -105,8 +105,8 @@ npm run build
|
||||
# publishing to npm & GitHub releases
|
||||
# uses https://github.com/webpro/release-it
|
||||
npm run release
|
||||
npm run release-minor
|
||||
npm run release-major
|
||||
npm run release minor
|
||||
npm run release major
|
||||
```
|
||||
|
||||
## Changelog
|
||||
|
13
package.json
13
package.json
@ -7,12 +7,10 @@
|
||||
"build": "babel src --out-dir . --ignore __tests__",
|
||||
"start": "babel -w src --out-dir . --ignore __tests__",
|
||||
"test": "eslint ./src/**/*.js",
|
||||
"release": "./node_modules/release-it/bin/release-it.js --src.tagName='v%s' --github.release --npm.publish --non-interactive",
|
||||
"release-minor": "./node_modules/release-it/bin/release-it.js minor --src.tagName='v%s' --github.release --npm.publish --non-interactive",
|
||||
"release-major": "./node_modules/release-it/bin/release-it.js major --src.tagName='v%s' --github.release --npm.publish --non-interactive",
|
||||
"format": "prettier --write 'src/**/*.{js,jsx}'",
|
||||
"release": "release-it --non-interactive",
|
||||
"prepublishOnly": "cross-env NODE_ENV=production npm run build",
|
||||
"changelog": "./node_modules/.bin/conventional-changelog -i CHANGELOG.md -s -r 0",
|
||||
"beforeStage": "npm run changelog"
|
||||
"changelog": "./node_modules/.bin/conventional-changelog -i CHANGELOG.md -s -r 0"
|
||||
},
|
||||
"dependencies": {},
|
||||
"devDependencies": {
|
||||
@ -23,11 +21,14 @@
|
||||
"conventional-changelog-cli": "^2.0.12",
|
||||
"cross-env": "^5.2.0",
|
||||
"eslint": "^5.16.0",
|
||||
"eslint-config-prettier": "^4.3.0",
|
||||
"eslint-plugin-prettier": "^3.1.0",
|
||||
"eslint-plugin-react": "^7.12.4",
|
||||
"prettier": "^1.18.2",
|
||||
"react": "^16.8.6",
|
||||
"react-dom": "^16.8.6",
|
||||
"react-hot-loader": "^4.8.1",
|
||||
"release-it": "^10.0.0"
|
||||
"release-it": "^12.3.0"
|
||||
},
|
||||
"homepage": "https://github.com/kremalicious/gatsby-plugin-matomo",
|
||||
"keywords": [
|
||||
|
@ -15,12 +15,17 @@ function getDuration() {
|
||||
}
|
||||
|
||||
exports.onRouteUpdate = ({ location, prevLocation }) => {
|
||||
if (process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined' || window.dev === true) {
|
||||
if (
|
||||
(process.env.NODE_ENV === 'production' && typeof _paq !== 'undefined') ||
|
||||
window.dev === true
|
||||
) {
|
||||
const _paq = window._paq || []
|
||||
const dev = window.dev || null
|
||||
|
||||
const url = location.pathname + location.search + location.hash
|
||||
const prevUrl = prevLocation && prevLocation.pathname + prevLocation.search + prevLocation.hash
|
||||
const prevUrl =
|
||||
prevLocation &&
|
||||
prevLocation.pathname + prevLocation.search + prevLocation.hash
|
||||
|
||||
// document.title workaround stolen from:
|
||||
// https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby-plugin-google-analytics/src/gatsby-browser.js
|
||||
@ -49,13 +54,7 @@ exports.onRouteUpdate = ({ location, prevLocation }) => {
|
||||
|
||||
if (first) {
|
||||
first = false
|
||||
_paq.push([
|
||||
'trackEvent',
|
||||
'javascript',
|
||||
'load',
|
||||
'duration',
|
||||
getDuration()
|
||||
])
|
||||
_paq.push(['trackEvent', 'javascript', 'load', 'duration', getDuration()])
|
||||
|
||||
if (dev) {
|
||||
console.log(`[Matomo] Tracking duration for: ${url}`)
|
||||
|
@ -1,18 +1,25 @@
|
||||
import React from 'react'
|
||||
|
||||
function buildTrackingCode(pluginOptions) {
|
||||
const script = pluginOptions.localScript
|
||||
? pluginOptions.localScript
|
||||
: `${pluginOptions.matomoUrl}/piwik.js`
|
||||
const {
|
||||
matomoUrl,
|
||||
siteId,
|
||||
dev,
|
||||
localScript,
|
||||
requireConsent,
|
||||
disableCookies
|
||||
} = pluginOptions
|
||||
|
||||
const script = localScript ? localScript : `${matomoUrl}/piwik.js`
|
||||
|
||||
const html = `
|
||||
window.dev = ${pluginOptions.dev}
|
||||
window.dev = ${dev}
|
||||
if (window.dev === true || !(navigator.doNotTrack === '1' || window.doNotTrack === '1')) {
|
||||
window._paq = window._paq || [];
|
||||
${pluginOptions.requireConsent ? 'window._paq.push([\'requireConsent\']);' : ''}
|
||||
${pluginOptions.disableCookies ? 'window._paq.push([\'disableCookies\']);' : ''}
|
||||
window._paq.push(['setTrackerUrl', '${pluginOptions.matomoUrl}/piwik.php']);
|
||||
window._paq.push(['setSiteId', '${pluginOptions.siteId}']);
|
||||
${requireConsent ? "window._paq.push(['requireConsent']);" : ''}
|
||||
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
|
||||
window._paq.push(['setTrackerUrl', '${matomoUrl}/piwik.php']);
|
||||
window._paq.push(['setSiteId', '${siteId}']);
|
||||
window._paq.push(['enableHeartBeatTimer']);
|
||||
window.start = new Date();
|
||||
|
||||
@ -23,7 +30,7 @@ function buildTrackingCode(pluginOptions) {
|
||||
|
||||
if (window.dev === true) {
|
||||
console.log('[Matomo] Tracking initialized')
|
||||
console.log('[Matomo] matomoUrl: ${pluginOptions.matomoUrl}, siteId: ${pluginOptions.siteId}')
|
||||
console.log('[Matomo] matomoUrl: ${matomoUrl}, siteId: ${siteId}')
|
||||
}
|
||||
}
|
||||
`
|
||||
@ -37,7 +44,10 @@ function buildTrackingCode(pluginOptions) {
|
||||
}
|
||||
|
||||
function buildTrackingCodeNoJs(pluginOptions, pathname) {
|
||||
const html = `<img src="${pluginOptions.matomoUrl}/piwik.php?idsite=${pluginOptions.siteId}&rec=1&url=${pluginOptions.siteUrl + pathname}" style="border:0" alt="tracker" />`
|
||||
const html = `<img src="${pluginOptions.matomoUrl}/piwik.php?idsite=${
|
||||
pluginOptions.siteId
|
||||
}&rec=1&url=${pluginOptions.siteUrl +
|
||||
pathname}" style="border:0" alt="tracker" />`
|
||||
|
||||
return (
|
||||
<noscript
|
||||
|
Loading…
Reference in New Issue
Block a user