diff --git a/.changeset/eight-candles-tie.md b/.changeset/eight-candles-tie.md new file mode 100644 index 000000000..7521cfe2f --- /dev/null +++ b/.changeset/eight-candles-tie.md @@ -0,0 +1,5 @@ +--- +'@gitbook/integration-hubspot': major +--- + +Add HubSpot integration diff --git a/.changeset/empty-dragons-love.md b/.changeset/empty-dragons-love.md new file mode 100644 index 000000000..6498b39fb --- /dev/null +++ b/.changeset/empty-dragons-love.md @@ -0,0 +1,5 @@ +--- +'@gitbook/integration-hubspot': major +--- + +Initial publish of the GitBook Hubspot integration diff --git a/integrations/hubspot/.eslintrc.json b/integrations/hubspot/.eslintrc.json new file mode 100644 index 000000000..2486b4b2d --- /dev/null +++ b/integrations/hubspot/.eslintrc.json @@ -0,0 +1,3 @@ +{ + "extends": ["@gitbook/eslint-config/integration"] +} diff --git a/integrations/hubspot/CHANGELOG.md b/integrations/hubspot/CHANGELOG.md new file mode 100644 index 000000000..c6ba1b3d3 --- /dev/null +++ b/integrations/hubspot/CHANGELOG.md @@ -0,0 +1,9 @@ +# @gitbook/integration-hubspot + +## 1.0.0 + +### Major Changes + +### Minor Changes + +### Patch Changes diff --git a/integrations/hubspot/assets/hubspot-preview.png b/integrations/hubspot/assets/hubspot-preview.png new file mode 100644 index 000000000..71ba2b596 Binary files /dev/null and b/integrations/hubspot/assets/hubspot-preview.png differ diff --git a/integrations/hubspot/assets/icon.png b/integrations/hubspot/assets/icon.png new file mode 100644 index 000000000..8b3e29654 Binary files /dev/null and b/integrations/hubspot/assets/icon.png differ diff --git a/integrations/hubspot/gitbook-manifest.yaml b/integrations/hubspot/gitbook-manifest.yaml new file mode 100644 index 000000000..be2eae138 --- /dev/null +++ b/integrations/hubspot/gitbook-manifest.yaml @@ -0,0 +1,48 @@ +name: hubspot +title: HubSpot +icon: ./assets/icon.png +previewImages: + - ./assets/hubspot-preview.png +organization: d8f63b60-89ae-11e7-8574-5927d48c4877 +description: Plug your GitBook site to your HubSpot installation. +visibility: public +script: ./src/index.ts +# The following scope(s) are available only to GitBook Staff +# See https://developer.gitbook.com/integrations/configurations#scopes +scopes: + - space:script:inject + - site:script:inject +contentSecurityPolicy: + script-src: hsadspixel.net hs-banner.com hs-analytics.net hs-scripts.com; +summary: | + # Overview + This integration allows to add the HubSpot tracking code on your published GitBook site. + + # How it works + The integration injects the HubSpot script on your page, using the configured environment ID, + so that you can get analytics information from your GitBook site directly inside of Heap. + + # Configure + Install the integration on the GitBook space of your choice. + Locate the HubSpot Script Loader ID you want to use, which is available in HubSpot's Reports & Analytics Tracking section. + + The ID should be just the id - "//js.hs-scripts.com/{hubspot_id}.js". (i.e. only paste what you see in hubspot_id) +categories: + - analytics +configurations: + space: + properties: + script_loader_url: + type: string + title: HubSpot Script Loader URL + description: The URL to load the script from. Copied form HubSpot's script tracking code script tag src attribute + required: + - script_loader_url + site: + properties: + script_loader_url: + type: string + title: HubSpot Script Loader URL + description: The URL to load the script from. Copied form HubSpot's script tracking code script tag src attribute + required: + - script_loader_url diff --git a/integrations/hubspot/package.json b/integrations/hubspot/package.json new file mode 100644 index 000000000..046727050 --- /dev/null +++ b/integrations/hubspot/package.json @@ -0,0 +1,18 @@ +{ + "name": "@gitbook/integration-hubspot", + "version": "1.0.0", + "private": true, + "dependencies": { + "@gitbook/api": "*", + "@gitbook/runtime": "*" + }, + "devDependencies": { + "@gitbook/cli": "*" + }, + "scripts": { + "lint": "eslint ./src/**/*.ts", + "typecheck": "tsc --noEmit", + "publish-integrations-staging": "gitbook publish .", + "publish-integrations": "gitbook publish ." + } +} \ No newline at end of file diff --git a/integrations/hubspot/src/index.ts b/integrations/hubspot/src/index.ts new file mode 100644 index 000000000..03b70be24 --- /dev/null +++ b/integrations/hubspot/src/index.ts @@ -0,0 +1,43 @@ +import { + createIntegration, + FetchPublishScriptEventCallback, + RuntimeContext, + RuntimeEnvironment, +} from '@gitbook/runtime'; + +import script from './script.raw.js'; + +type HubSpotRuntimeContext = RuntimeContext< + RuntimeEnvironment< + {}, + { + script_loader_url?: string; + } + > +>; + +export const handleFetchEvent: FetchPublishScriptEventCallback = ( + event, + { environment }: HubSpotRuntimeContext +) => { + const scriptLoaderURL = + environment.siteInstallation?.configuration?.script_loader_url ?? + environment.spaceInstallation.configuration.script_loader_url; + + if (!scriptLoaderURL) { + throw new Error( + `The HubSpot Script Loader URL is missing from the configuration (ID: ${event.spaceId}).` + ); + } + + return new Response(script.replace('', scriptLoaderURL), { + headers: { + 'Content-Type': 'application/javascript', + 'Cache-Control': 'max-age=604800', + }, + }); +}; + +export default createIntegration({ + fetch_published_script: handleFetchEvent, +}); diff --git a/integrations/hubspot/src/script.raw.js b/integrations/hubspot/src/script.raw.js new file mode 100644 index 000000000..11cc8e1c9 --- /dev/null +++ b/integrations/hubspot/src/script.raw.js @@ -0,0 +1,25 @@ +function loadScript() { + if (!document.getElementById('hs-script-loader')) { + const trackingID = ''; + + const scriptLoaderURL = '//js.hs-scripts.com/' + trackingID + '.js'; + const headTag = document.getElementsByTagName('head')[0]; + const scriptTag = document.createElement('script'); + scriptTag.src = scriptLoaderURL; + scriptTag.type = 'text/javascript'; + scriptTag.id = 'hs-script-loader'; + scriptTag.async = 1; + scriptTag.defer = 1; + headTag.appendChild(scriptTag); + const o = (window._hsp = window._hsp || []); + o.push(['setContentType', 'knowledge-article']); + } +} + +(function () { + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', loadScript); + } else { + loadScript(); + } +})(); diff --git a/integrations/hubspot/tsconfig.json b/integrations/hubspot/tsconfig.json new file mode 100644 index 000000000..f839d1833 --- /dev/null +++ b/integrations/hubspot/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@gitbook/tsconfig/integration.json" +} \ No newline at end of file