Skip to content

Commit 2d3096d

Browse files
authored
Fix plausible script tags and make them configurable (#105)
* Add plausible through metadata * Use Gatsby SiteMetadata for plausible domain and src * Use automatic plausible inference and relative proxy source links * Disable plausible on example * v0.1.23 * Add plausibleAPI * Re-disable plausible in example * Fix conditional plausible disable * Re-add defaults for max compatibility * Use disablePlausible flag and change mechanism to signal with src * Use pluginOptions for src/api/domain and use src presence over extra boolean flag * Allow null on plausible options * Change src to null
1 parent b5812d2 commit 2d3096d

File tree

7 files changed

+87
-38
lines changed

7 files changed

+87
-38
lines changed

packages/example/gatsby-config.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ module.exports = {
99
title: 'Example website',
1010
description: 'Example website description',
1111
keywords: ['docs', 'test'],
12-
siteUrl: 'http://localhost:8000'
12+
siteUrl: 'http://gatsby-theme-iterative-example.herokuapp.com'
1313
},
1414
plugins: [
1515
{
1616
resolve: themePackageName,
1717
options: {
18-
simpleLinkerTerms: require('./content/linked-terms')
18+
simpleLinkerTerms: require('./content/linked-terms'),
19+
plausibleSrc: null
1920
}
2021
},
2122
{

packages/gatsby-theme-iterative/gatsby-config.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ const imageMaxWidth = 700
3333

3434
module.exports = ({
3535
simpleLinkerTerms,
36+
plausibleSrc = '/pl/js/plausible.outbound-links.js',
37+
plausibleAPI = '/pl/api/event',
38+
plausibleDomain,
3639
cssBase = defaultCssBase,
3740
customMediaConfig = { importFrom: [mediaConfig] },
3841
customPropertiesConfig = {
@@ -172,7 +175,9 @@ module.exports = ({
172175
],
173176
siteMetadata: {
174177
author: 'Iterative',
175-
siteUrl: 'https://cml.dev',
176-
titleTemplate: ''
178+
titleTemplate: '',
179+
plausibleSrc,
180+
plausibleAPI,
181+
plausibleDomain
177182
}
178183
})

packages/gatsby-theme-iterative/gatsby-node.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ exports.pluginOptionsSchema = ({ Joi }) => {
2626
customMediaConfig: Joi.object(),
2727
customPropertiesConfig: Joi.object(),
2828
colorModConfig: Joi.object(),
29-
postCssPlugins: Joi.array()
29+
postCssPlugins: Joi.array(),
30+
plausibleSrc: [Joi.string().optional(), Joi.allow(null)],
31+
plausibleAPI: [Joi.string().optional(), Joi.allow(null)],
32+
plausibleDomain: [Joi.string().optional(), Joi.allow(null)]
3033
})
3134
}
3235

@@ -57,6 +60,17 @@ exports.createSchemaCustomization = async api => {
5760
name: 'String!',
5861
match: '[String]'
5962
}
63+
}),
64+
buildObjectType({
65+
name: 'SiteSiteMetadata',
66+
fields: {
67+
author: 'String',
68+
siteUrl: 'String',
69+
titleTemplate: 'String',
70+
plausibleSrc: 'String',
71+
plausibleDomain: 'String',
72+
plausibleAPI: 'String'
73+
}
6074
})
6175
])
6276
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
/* eslint-env node */
2+
const React = require('react')
23

34
const PageWrapper = require('./src/components/PageWrapper').default
45

56
exports.wrapPageElement = PageWrapper
7+
8+
const onRenderBody = ({ setHeadComponents }, { plausibleSrc }) => {
9+
if (plausibleSrc) {
10+
return setHeadComponents([
11+
<script
12+
key="plausible-custom-events"
13+
dangerouslySetInnerHTML={{
14+
__html:
15+
'window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }'
16+
}}
17+
/>
18+
])
19+
}
20+
}
21+
22+
exports.onRenderBody = onRenderBody

packages/gatsby-theme-iterative/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@dvcorg/gatsby-theme-iterative",
3-
"version": "0.1.22",
3+
"version": "0.1.23",
44
"description": "",
55
"main": "index.js",
66
"types": "src/typings.d.ts",

packages/gatsby-theme-iterative/src/components/Page/DefaultSEO/index.tsx

+38-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react'
2-
import { Script } from 'gatsby'
32
import Helmet from 'react-helmet'
43

54
import { MetaProps } from '../../SEO'
@@ -17,11 +16,16 @@ const metaImage = {
1716

1817
const DefaultSEO: React.FC<IDefaultSEOProps> = ({ pathname }) => {
1918
const siteMeta = getSiteMeta()
20-
const siteUrl = siteMeta.siteUrl
21-
const metaTitle = siteMeta.title
22-
const metaTitleTemplate = siteMeta.titleTemplate
23-
const metaDescription = siteMeta.description
24-
const metaKeywords = siteMeta.keywords
19+
const {
20+
siteUrl,
21+
titleTemplate,
22+
title: metaTitle,
23+
description: metaDescription,
24+
keywords: metaKeywords,
25+
plausibleDomain,
26+
plausibleSrc,
27+
plausibleAPI
28+
} = siteMeta
2529
const fullUrl = siteUrl + pathname
2630

2731
const meta: MetaProps[] = [
@@ -104,32 +108,34 @@ const DefaultSEO: React.FC<IDefaultSEOProps> = ({ pathname }) => {
104108
]
105109

106110
return (
107-
<>
108-
<Helmet
109-
htmlAttributes={{
110-
lang: 'en'
111-
}}
112-
defaultTitle={metaTitle}
113-
titleTemplate={metaTitleTemplate || `%s | ${metaTitle}`}
114-
meta={meta}
115-
link={[
116-
{
117-
rel: 'mask-icon',
118-
href: '/safari-pinned-tab.svg',
119-
color: '#13adc7'
120-
},
121-
{
122-
rel: 'canonical',
123-
href: fullUrl
124-
}
125-
]}
126-
/>
127-
<Script
128-
defer
129-
data-domain="dvc.org"
130-
src="https://plausible.io/js/plausible.outbound-links.js"
131-
/>
132-
</>
111+
<Helmet
112+
htmlAttributes={{
113+
lang: 'en'
114+
}}
115+
defaultTitle={metaTitle}
116+
titleTemplate={titleTemplate || `%s | ${metaTitle}`}
117+
meta={meta}
118+
link={[
119+
{
120+
rel: 'mask-icon',
121+
href: '/safari-pinned-tab.svg',
122+
color: '#13adc7'
123+
},
124+
{
125+
rel: 'canonical',
126+
href: fullUrl
127+
}
128+
]}
129+
>
130+
{plausibleSrc ? (
131+
<script
132+
defer
133+
data-domain={plausibleDomain || new URL(siteUrl).hostname}
134+
data-api={plausibleAPI || undefined}
135+
src={plausibleSrc}
136+
/>
137+
) : null}
138+
</Helmet>
133139
)
134140
}
135141

packages/gatsby-theme-iterative/src/queries/siteMeta.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ interface ISiteMeta {
66
keywords: string
77
siteUrl: string
88
titleTemplate: string
9+
plausibleDomain: string | null
10+
plausibleSrc: string | null
11+
plausibleAPI: string | null
912
}
1013

1114
export default function siteMeta(): ISiteMeta {
@@ -21,6 +24,9 @@ export default function siteMeta(): ISiteMeta {
2124
keywords
2225
siteUrl
2326
titleTemplate
27+
plausibleDomain
28+
plausibleSrc
29+
plausibleAPI
2430
}
2531
}
2632
}

0 commit comments

Comments
 (0)