diff --git a/.changeset/slow-keys-confess.md b/.changeset/slow-keys-confess.md new file mode 100644 index 000000000..b69c86ae7 --- /dev/null +++ b/.changeset/slow-keys-confess.md @@ -0,0 +1,5 @@ +--- +'@gitbook/integration-googleanalytics': minor +--- + +Update Google Analytics integration configuration diff --git a/integrations/googleanalytics/gitbook-manifest.yaml b/integrations/googleanalytics/gitbook-manifest.yaml index cc77c2d2e..01b9d99cd 100644 --- a/integrations/googleanalytics/gitbook-manifest.yaml +++ b/integrations/googleanalytics/gitbook-manifest.yaml @@ -13,8 +13,6 @@ 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 - - space:script:cookies - site:script:inject - site:script:cookies contentSecurityPolicy: @@ -53,10 +51,4 @@ categories: - analytics configurations: site: - properties: - tracking_id: - type: string - title: Tracking ID - description: Look for this in your Google Analytics account. - required: - - tracking_id + componentId: config diff --git a/integrations/googleanalytics/src/components.tsx b/integrations/googleanalytics/src/components.tsx new file mode 100644 index 000000000..1ee76982b --- /dev/null +++ b/integrations/googleanalytics/src/components.tsx @@ -0,0 +1,83 @@ +import { createComponent, RuntimeContext, RuntimeEnvironment } from '@gitbook/runtime'; + +type GARuntimeEnvironment = RuntimeEnvironment<{}, GASiteInstallationConfiguration>; + +type GARuntimeContext = RuntimeContext; + +type GASiteInstallationConfiguration = { + tracking_id?: string; +}; + +type GAState = GASiteInstallationConfiguration; + +type GAProps = { + siteInstallation: { + configuration?: GASiteInstallationConfiguration; + }; +}; + +export type GAAction = { action: 'save.config' }; + +export const configBlock = createComponent({ + componentId: 'config', + initialState: (props) => { + const siteInstallation = props.siteInstallation; + return { + tracking_id: siteInstallation.configuration?.tracking_id ?? '', + }; + }, + action: async (element, action, context) => { + switch (action.action) { + case 'save.config': + const { api, environment } = context; + const siteInstallation = environment.siteInstallation; + if (!siteInstallation) { + throw Error('No site installation found'); + } + + const configurationBody = { + ...siteInstallation.configuration, + tracking_id: element.state.tracking_id, + }; + await api.integrations.updateIntegrationSiteInstallation( + siteInstallation.integration, + siteInstallation.installation, + siteInstallation.site, + { + configuration: { + ...configurationBody, + }, + }, + ); + return element; + } + }, + render: async () => { + return ( + + The unique identifier of your GA app client.} + element={} + /> + + + + } + /> + + ); + }, +}); diff --git a/integrations/googleanalytics/src/index.ts b/integrations/googleanalytics/src/index.ts index 40a6dfb7d..8adef5b2f 100644 --- a/integrations/googleanalytics/src/index.ts +++ b/integrations/googleanalytics/src/index.ts @@ -5,6 +5,7 @@ import { RuntimeEnvironment, } from '@gitbook/runtime'; +import { configBlock } from './components'; import script from './script.raw.js'; type GARuntimeContext = RuntimeContext< @@ -35,4 +36,5 @@ export const handleFetchEvent: FetchPublishScriptEventCallback = async ( export default createIntegration({ fetch_published_script: handleFetchEvent, + components: [configBlock], });