Skip to content

Commit 629603e

Browse files
Add initial code for crisp integration
1 parent fc4c6dc commit 629603e

File tree

7 files changed

+134
-0
lines changed

7 files changed

+134
-0
lines changed

integrations/crisp/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": ["@gitbook/eslint-config/integration"]
3+
}

integrations/crisp/assets/icon.png

21.4 KB
Loading
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: crisp
2+
title: Crisp
3+
icon: ./assets/icon.png
4+
description: Add the Crisp chat widget to your published GitBook content.
5+
externalLinks:
6+
- label: Website
7+
url: https://crisp.chat/en/
8+
visibility: public
9+
script: ./src/index.ts
10+
# The following scope(s) are available only to GitBook Staff
11+
# See https://developer.gitbook.com/integrations/configurations#scopes
12+
scopes:
13+
- space:script:inject
14+
- site:script:inject
15+
organization: gitbook
16+
contentSecurityPolicy:
17+
script-src: client.crisp.chat;
18+
summary: |
19+
# Overview
20+
21+
The Crisp integration for GitBook allows you to display the Crisp chat widget on your public documentation to connect and interact with your readers.
22+
23+
# How it works
24+
25+
Automatic chat widget on your documentation: Each of your connected GitBook spaces will fetch the Crisp chat widget script and inject it in your published content.
26+
27+
# Configure
28+
29+
You can configure the integration on single or multiple public spaces by navigating to the integrations in sub-navigation or org settings. You will then have to provide Crisp website ID to finish the configuration. This can be found in the URL from your Crisp dashboard.
30+
31+
categories:
32+
- analytics
33+
configurations:
34+
space:
35+
properties:
36+
tracking_id:
37+
type: string
38+
title: Crisp Site ID
39+
description: Available in the URL from your Crisp dashboard
40+
required:
41+
- tracking_id
42+
site:
43+
properties:
44+
tracking_id:
45+
type: string
46+
title: Crisp Site ID
47+
description: Available in the URL from your Crisp dashboard
48+
required:
49+
- tracking_id

integrations/crisp/package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "@gitbook/integration-crisp",
3+
"version": "0.0.1",
4+
"private": true,
5+
"dependencies": {
6+
"@gitbook/api": "*",
7+
"@gitbook/runtime": "*"
8+
},
9+
"devDependencies": {
10+
"@gitbook/cli": "*"
11+
},
12+
"scripts": {
13+
"lint": "eslint ./src/**/*.ts",
14+
"typecheck": "tsc --noEmit",
15+
"publish-integrations-staging": "gitbook publish .",
16+
"publish-integrations": "gitbook publish ."
17+
}
18+
}

integrations/crisp/src/index.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {
2+
createIntegration,
3+
FetchPublishScriptEventCallback,
4+
RuntimeContext,
5+
RuntimeEnvironment,
6+
} from '@gitbook/runtime';
7+
8+
import script from './script.raw.js';
9+
10+
type CrispRuntimeContext = RuntimeContext<
11+
RuntimeEnvironment<
12+
{},
13+
{
14+
tracking_id?: string;
15+
}
16+
>
17+
>;
18+
19+
export const handleFetchEvent: FetchPublishScriptEventCallback = async (
20+
event,
21+
{ environment }: CrispRuntimeContext
22+
) => {
23+
const trackingId =
24+
environment.siteInstallation?.configuration?.tracking_id ??
25+
environment.spaceInstallation.configuration.tracking_id;
26+
if (!trackingId) {
27+
throw new Error(
28+
`The Crisp Website ID is missing from the configuration (ID: ${
29+
'spaceId' in event ? event.spaceId : event.siteId
30+
}).`
31+
);
32+
}
33+
34+
return new Response(script.replace('<TO_REPLACE>', trackingId), {
35+
headers: {
36+
'Content-Type': 'application/javascript',
37+
'Cache-Control': 'max-age=604800',
38+
},
39+
});
40+
};
41+
42+
export default createIntegration<CrispRuntimeContext>({
43+
fetch_published_script: handleFetchEvent,
44+
});

integrations/crisp/src/script.raw.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// (function (h, o, t, j, a, r) {
2+
// const trackingID = '<TO_REPLACE>';
3+
// h.hj =
4+
// h.hj ||
5+
// function () {
6+
// (h.hj.q = h.hj.q || []).push(arguments);
7+
// };
8+
// h._hjSettings = { hjid: trackingID, hjsv: 6 };
9+
// a = o.getElementsByTagName('head')[0];
10+
// r = o.createElement('script');
11+
// r.async = 1;
12+
// r.src = t + h._hjSettings.hjid + j + h._hjSettings.hjsv;
13+
// a.appendChild(r);
14+
// })(window, document, 'https://static.hotjar.com/c/hotjar-', '.js?sv=');
15+
16+
// Example script
17+
// <script type="text/javascript">window.$crisp=[];window.CRISP_WEBSITE_ID="<TO_REPLACE>";(function(){d=document;s=d.createElement("script");s.src="https://client.crisp.chat/l.js";s.async=1;d.getElementsByTagName("head")[0].appendChild(s);})();</script>

integrations/crisp/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "@gitbook/tsconfig/integration.json"
3+
}

0 commit comments

Comments
 (0)