Gatsby Plugin for Matomo
Integrates your site with Matomo.
Install
yarn add @whitespace/gatsby-plugin-matomo
How to use
plugins: [
{
resolve: "gatsby-plugin-matomo",
options: {
mtmHost: "YOUR_SELF_HOSTED_ORIGIN",
mtmContainerId: "YOUR_MATOMO_CONTAINER_ID",
includeInDevelopment: false,
routeChangeEventName: "gatsby-route-change",
trackPageViews: false,
requireCookieConsent: false,
},
},
];
Integration with @whitespace/gatsby-plugin-cookie-consent
If you are using @whitespace/gatsby-plugin-cookie-consent it’s recommended to
whitelist the container url and hook into the store to check if consent has been
given:
- Add this to
gatsby-browser.js:
import MatomoCookieHandler from "./src/components/MatomoCookieHandler";
export function wrapPageElement({ element }) {
return (
<>
<MatomoCookieHandler />
{element}
</>
);
}
- And this to
src/components/MatomoCookieHandler.js:
import { useStore } from "@whitespace/gatsby-plugin-cookie-consent/src/hooks/store";
import React, { useEffect } from "react";
MatomoCookieHandler.propTypes = {};
export default function MatomoCookieHandler() {
let [store] = useStore();
let cookiesAreAccepted = store.answer === "accept";
useEffect(() => {
if (cookiesAreAccepted && typeof window !== "undefined") {
window._paq.push(["setCookieConsentGiven"]);
}
}, [cookiesAreAccepted]);
return null;
}