Skip to content

Commit

Permalink
Merge pull request #24 from ArneZsng/master
Browse files Browse the repository at this point in the history
Add support for custom JS and PHP script names
  • Loading branch information
kremalicious authored Mar 22, 2020
2 parents 777be77 + 4a0beae commit 30a5836
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ _NOTE: By default, this plugin only generates output when run in production mode
| `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. |
| `matomoPhpScript`| (optional) The name of your Matomo PHP script. Defaults to `piwik.php` |
| `matomoJsScript` | (optional) The name of your Matomo JS script. Defaults to `piwik.js` |
| `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. |
Expand All @@ -86,6 +88,8 @@ plugins: [
matomoUrl: 'https://YOUR_MATOMO_URL.COM',
siteUrl: 'https://YOUR_LIVE_SITE_URL.COM',
// All the optional settings
matomoPhpScript: 'piwik.php',
matomoJsScript: 'piwik.js',
exclude: ['/offline-plugin-app-shell-fallback/'],
requireConsent: false,
disableCookies: false,
Expand Down
18 changes: 10 additions & 8 deletions src/gatsby-ssr.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import React from 'react'
function buildTrackingCode(pluginOptions) {
const {
matomoUrl,
matomoPhpScript = 'piwik.php',
matomoJsScript = 'piwik.js',
siteId,
dev,
localScript,
Expand All @@ -12,7 +14,7 @@ function buildTrackingCode(pluginOptions) {
cookieDomain
} = pluginOptions

const script = localScript ? localScript : `${matomoUrl}/piwik.js`
const script = localScript ? localScript : `${matomoUrl}/${matomoJsScript}`

const html = `
window.dev = ${dev}
Expand All @@ -21,11 +23,11 @@ function buildTrackingCode(pluginOptions) {
${requireConsent ? "window._paq.push(['requireConsent']);" : ''}
${disableCookies ? "window._paq.push(['disableCookies']);" : ''}
${
cookieDomain
? `window._paq.push(['setCookieDomain', '${cookieDomain}']);`
: ''
}
window._paq.push(['setTrackerUrl', '${matomoUrl}/piwik.php']);
cookieDomain
? `window._paq.push(['setCookieDomain', '${cookieDomain}']);`
: ''
}
window._paq.push(['setTrackerUrl', '${matomoUrl}/${matomoPhpScript}']);
window._paq.push(['setSiteId', '${siteId}']);
window._paq.push(['enableHeartBeatTimer']);
window.start = new Date();
Expand All @@ -51,8 +53,8 @@ function buildTrackingCode(pluginOptions) {
}

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

return (
Expand Down

0 comments on commit 30a5836

Please sign in to comment.