Skip to content

Commit cbae79a

Browse files
Fix/plausible init (#1219)
* Update Plausible integration to use Script instead of API * Fix manifest description lenght * Reorder script inject * update changeset
1 parent 4d15eef commit cbae79a

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

‎.changeset/loose-things-care.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/integration-plausible': patch
3+
---
4+
5+
Change to patch

‎integrations/plausible/src/index.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async (
2222
{ environment }: PlausibleRuntimeContext,
2323
) => {
2424
const scriptKey = environment.siteInstallation?.configuration?.scriptKey;
25-
const selfHostedUrl = environment.siteInstallation?.configuration?.selfHostedUrl || '';
25+
const selfHostedUrl = environment.siteInstallation?.configuration?.selfHostedUrl;
2626
if (!scriptKey) {
2727
return;
2828
}
2929

30-
const baseUrl = selfHostedUrl || 'https://plausible.io';
31-
const scriptSrc = `${baseUrl}/js/${scriptKey}.js`;
30+
const normalizedKey = scriptKey.startsWith('pa-') ? scriptKey : `pa-${scriptKey}`;
31+
const baseUrl = (selfHostedUrl || 'https://plausible.io').replace(/\/+$/, '');
32+
const scriptSrc = `${baseUrl}/js/${normalizedKey}.js`;
3233

3334
return new Response((script as string).replace('<scriptSrc>', scriptSrc), {
3435
headers: {
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
(function () {
1+
(function (doc) {
2+
var js = doc.createElement('script');
3+
js.async = true;
4+
js.src = '<scriptSrc>';
5+
var first = doc.getElementsByTagName('script')[0];
6+
if (first && first.parentNode) {
7+
first.parentNode.insertBefore(js, first);
8+
} else {
9+
(doc.head || doc.documentElement).appendChild(js);
10+
}
11+
212
window.plausible =
313
window.plausible ||
414
function () {
@@ -10,9 +20,4 @@
1020
plausible.o = i || {};
1121
};
1222
plausible.init();
13-
14-
var s = document.createElement('script');
15-
s.async = true;
16-
s.src = '<scriptSrc>';
17-
document.head.appendChild(s);
18-
})();
23+
})(document);

0 commit comments

Comments
 (0)