Skip to content

Commit ed953c1

Browse files
committed
Create a gatsby plugin for plausible
1 parent fe4d44e commit ed953c1

File tree

4 files changed

+42
-0
lines changed

4 files changed

+42
-0
lines changed

gatsby-config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = {
2222
"gatsby-plugin-styled-components",
2323
"gatsby-plugin-react-helmet",
2424
"custom-bytes-subscriber-count-plugin",
25+
"custom-plausible-plugin",
2526
{
2627
resolve: "gatsby-source-filesystem",
2728
options: {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
exports.onRouteUpdate = function ({ location }) {
2+
if (
3+
process.env.NODE_ENV === `production` &&
4+
typeof window.plausible === `object`
5+
) {
6+
const pathIsExcluded =
7+
location &&
8+
typeof window.plausibleExcludePaths !== `undefined` &&
9+
window.plausibleExcludePaths.some((rx) => rx.test(location.pathname));
10+
11+
if (pathIsExcluded) return null;
12+
13+
window.plausible("pageview");
14+
}
15+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const React = require("react");
2+
3+
exports.onRenderBody = ({ setHeadComponents }) => {
4+
if (process.env.NODE_ENV === "production") {
5+
const scriptProps = {
6+
defer: true,
7+
"data-domain": "ui.dev",
8+
src: `https://pl-proxy.uidotdev.workers.dev/js/script.js`,
9+
};
10+
11+
return setHeadComponents([
12+
<script key="custom-plausible-plugin" {...scriptProps}></script>,
13+
<script
14+
key="custom-plausible-plugin-custom-events"
15+
dangerouslySetInnerHTML={{
16+
__html: `
17+
window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) };
18+
`,
19+
}}
20+
/>,
21+
]);
22+
}
23+
24+
return null;
25+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

0 commit comments

Comments
 (0)