Skip to content

Commit da7ad52

Browse files
Update Plausible integration to use Script instead of API (#1217)
* Update Plausible integration to use Script instead of API * Fix manifest description lenght
1 parent dc12238 commit da7ad52

4 files changed

Lines changed: 36 additions & 101 deletions

File tree

‎.changeset/sweet-grapes-end.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-plausible': major
3+
---
4+
5+
Update Plausible integration to use script instead of API

‎integrations/plausible/gitbook-manifest.yaml‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ script: ./src/index.ts
1515
scopes:
1616
- site:script:inject
1717
contentSecurityPolicy:
18-
script-src: https://plausible.io
18+
script-src: https://plausible.io *
1919
connect-src: https://plausible.io *
2020
summary: |
2121
# Overview
@@ -34,14 +34,14 @@ categories:
3434
configurations:
3535
site:
3636
properties:
37-
domain:
37+
scriptKey:
3838
type: string
39-
title: Domain
40-
description: The domain URL you configured for this Plausible website.
41-
api:
39+
title: Script key
40+
description: The "pa-XXXX" key from your Plausible snippet URL.
41+
selfHostedUrl:
4242
type: string
43-
title: Self-hosted API
44-
description: When self-hosting, configure the url of the instance API (empty when using plausible.io).
43+
title: Self-hosted URL
44+
description: Base URL of your self-hosted Plausible instance. Leave empty for plausible.io.
4545
required:
46-
- domain
46+
- scriptKey
4747
target: site

‎integrations/plausible/src/index.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ type PlausibleRuntimeContext = RuntimeContext<
1111
RuntimeEnvironment<
1212
{},
1313
{
14-
domain?: string;
15-
api?: string;
14+
scriptKey?: string;
15+
selfHostedUrl?: string;
1616
}
1717
>
1818
>;
@@ -21,13 +21,16 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async (
2121
event,
2222
{ environment }: PlausibleRuntimeContext,
2323
) => {
24-
const domain = environment.siteInstallation?.configuration?.domain;
25-
const api = environment.siteInstallation?.configuration?.api || '';
26-
if (!domain) {
24+
const scriptKey = environment.siteInstallation?.configuration?.scriptKey;
25+
const selfHostedUrl = environment.siteInstallation?.configuration?.selfHostedUrl || '';
26+
if (!scriptKey) {
2727
return;
2828
}
2929

30-
return new Response((script as string).replace('<domain>', domain).replace('<api>', api), {
30+
const baseUrl = selfHostedUrl || 'https://plausible.io';
31+
const scriptSrc = `${baseUrl}/js/${scriptKey}.js`;
32+
33+
return new Response((script as string).replace('<scriptSrc>', scriptSrc), {
3134
headers: {
3235
'Content-Type': 'application/javascript',
3336
'Cache-Control': 'max-age=604800',
Lines changed: 14 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,18 @@
1-
// Taken from plausible.io/js/script.js
21
(function () {
3-
const currentUrl = window.location;
4-
const document = window.document;
5-
6-
const apiUrl = '<api>' || 'https://plausible.io/api/event';
7-
const domain = '<domain>';
8-
9-
function logIgnoredEvent(event) {
10-
console.warn('Ignoring Event: ' + event);
11-
}
12-
13-
function trackEvent(eventName, options) {
14-
if (
15-
/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(currentUrl.hostname) ||
16-
'file:' === currentUrl.protocol
17-
) {
18-
return logIgnoredEvent('localhost');
19-
}
20-
21-
if (
22-
!(window._phantom || window.__nightmare || window.navigator.webdriver || window.Cypress)
23-
) {
24-
try {
25-
if (window.localStorage.plausible_ignore === 'true') {
26-
return logIgnoredEvent('localStorage flag');
27-
}
28-
} catch (error) {
29-
// do nothing
30-
}
31-
32-
const eventData = {};
33-
eventData.name = eventName;
34-
eventData.url = currentUrl.href;
35-
eventData.domain = domain;
36-
eventData.referrer = document.referrer || null;
37-
eventData.width = window.innerWidth;
38-
if (options && options.meta) {
39-
eventData.meta = JSON.stringify(options.meta);
40-
}
41-
if (options && options.props) {
42-
eventData.props = options.props;
43-
}
44-
45-
const xhr = new XMLHttpRequest();
46-
xhr.open('POST', apiUrl, true);
47-
xhr.setRequestHeader('Content-Type', 'text/plain');
48-
xhr.send(JSON.stringify(eventData));
49-
xhr.onreadystatechange = function () {
50-
if (xhr.readyState === 4 && options && options.callback) {
51-
options.callback();
52-
}
53-
};
54-
}
55-
}
56-
57-
const eventQueue = (window.plausible && window.plausible.q) || [];
58-
window.plausible = trackEvent;
59-
for (let i = 0; i < eventQueue.length; i++) {
60-
trackEvent.apply(this, eventQueue[i]);
61-
}
62-
63-
let currentPagePath;
64-
function trackPageView() {
65-
if (currentPagePath !== currentUrl.pathname) {
66-
currentPagePath = currentUrl.pathname;
67-
trackEvent('pageview');
68-
}
69-
}
70-
71-
let originalPushState;
72-
const history = window.history;
73-
if (history.pushState) {
74-
originalPushState = history.pushState;
75-
history.pushState = function () {
76-
originalPushState.apply(this, arguments);
77-
trackPageView();
2+
window.plausible =
3+
window.plausible ||
4+
function () {
5+
(plausible.q = plausible.q || []).push(arguments);
6+
};
7+
plausible.init =
8+
plausible.init ||
9+
function (i) {
10+
plausible.o = i || {};
7811
};
79-
window.addEventListener('popstate', trackPageView);
80-
}
12+
plausible.init();
8113

82-
if (document.visibilityState === 'prerender') {
83-
document.addEventListener('visibilitychange', function () {
84-
if (!currentPagePath && document.visibilityState === 'visible') {
85-
trackPageView();
86-
}
87-
});
88-
} else {
89-
trackPageView();
90-
}
14+
var s = document.createElement('script');
15+
s.async = true;
16+
s.src = '<scriptSrc>';
17+
document.head.appendChild(s);
9118
})();

0 commit comments

Comments
 (0)