Socket
Book a DemoInstallSign in
Socket

@whitespace/gatsby-plugin-matomo

Package Overview
Dependencies
Maintainers
8
Versions
136
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@whitespace/gatsby-plugin-matomo

Whitespace’s basic Gatsby plugin for Matomo

latest
npmnpm
Version
1.4.8
Version published
Weekly downloads
258
47.43%
Maintainers
8
Weekly downloads
 
Created
Source

Gatsby Plugin for Matomo

Integrates your site with Matomo.

Install

yarn add @whitespace/gatsby-plugin-matomo

How to use

// gatsby-config.js

plugins: [
  {
    resolve: "gatsby-plugin-matomo",
    options: {
      mtmHost: "YOUR_SELF_HOSTED_ORIGIN",
      mtmContainerId: "YOUR_MATOMO_CONTAINER_ID",
      includeInDevelopment: false,
      routeChangeEventName: "gatsby-route-change", // Set this `false` to disable events
      trackPageViews: false, // Change to `true` to track page views
      requireCookieConsent: false, // Change to true to require cookie consent. See below how to handle 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:
// gatsby-browser.js

import MatomoCookieHandler from "./src/components/MatomoCookieHandler";

export function wrapPageElement({ element }) {
  return (
    <>
      <MatomoCookieHandler />
      {element}
    </>
  );
}
  • And this to src/components/MatomoCookieHandler.js:
// src/components/MatomoCookieHandler.js

import { useStore } from "@whitespace/gatsby-plugin-cookie-consent/src/hooks/store";
import React, { useEffect } from "react";

MatomoCookieHandler.propTypes = {};

/**
 * Pushes "setCookieConsentGiven" to Matomo if/when cookies are accepted
 */
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;
}

FAQs

Package last updated on 22 May 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts