diff --git a/docusaurus.config.en.js b/docusaurus.config.en.js index 70bf7fb5319..c10c30dc7ac 100644 --- a/docusaurus.config.en.js +++ b/docusaurus.config.en.js @@ -14,6 +14,10 @@ function skipIndex(items) { /** @type {import('@docusaurus/types').Config} */ const config = { scripts: [ + { + src: "/docs/js/kapa_config.js", + async: false, + }, { src: "https://widget.kapa.ai/kapa-widget.bundle.js", "data-website-id": "c0b5f156-1e92-49df-8252-adacc9feb21b", diff --git a/docusaurus.config.jp.js b/docusaurus.config.jp.js index 7e9aa451bda..b8c416bd0a2 100644 --- a/docusaurus.config.jp.js +++ b/docusaurus.config.jp.js @@ -14,6 +14,10 @@ function skipIndex(items) { /** @type {import('@docusaurus/types').Config} */ const config = { scripts: [ + { + src: "/docs/jp/js/kapa_config.js", + async: false, + }, { src: "https://widget.kapa.ai/kapa-widget.bundle.js", "data-website-id": "c0b5f156-1e92-49df-8252-adacc9feb21b", diff --git a/docusaurus.config.ru.js b/docusaurus.config.ru.js index 47811ae02b3..8b4b1f62dae 100644 --- a/docusaurus.config.ru.js +++ b/docusaurus.config.ru.js @@ -14,6 +14,10 @@ function skipIndex(items) { /** @type {import('@docusaurus/types').Config} */ const config = { scripts: [ + { + src: "/docs/ru/js/kapa_config.js", + async: false, + }, { src: "https://widget.kapa.ai/kapa-widget.bundle.js", "data-website-id": "c0b5f156-1e92-49df-8252-adacc9feb21b", diff --git a/docusaurus.config.zh.js b/docusaurus.config.zh.js index 74ed6ffc7cc..2a4796733dd 100644 --- a/docusaurus.config.zh.js +++ b/docusaurus.config.zh.js @@ -14,6 +14,10 @@ function skipIndex(items) { /** @type {import('@docusaurus/types').Config} */ const config = { scripts: [ + { + src: "/docs/zh/js/kapa_config.js", + async: false, + }, { src: "https://widget.kapa.ai/kapa-widget.bundle.js", "data-website-id": "c0b5f156-1e92-49df-8252-adacc9feb21b", @@ -43,7 +47,7 @@ const config = { }, title: "ClickHouse Docs", tagline: - "我们提供文档、快速入门指南、用户指南、技术参考、常见问题解答等多种信息。", + "我们提供文档、快速入门指南、用户指南、技术参考、常见问题解答等多种信息。", url: "https://clickhouse.com", // url: process.env.VERCEL_URL ? `https://${process.env.VERCEL_URL}` : 'https://bookish-disco-5997zvo.pages.github.io', baseUrl: "/docs/zh/", diff --git a/static/js/kapa_config.js b/static/js/kapa_config.js new file mode 100644 index 00000000000..d8af6cebb0d --- /dev/null +++ b/static/js/kapa_config.js @@ -0,0 +1,42 @@ +/** + * Extracts the user ID from the Google Analytics device ID. + * @example `GA1.1.xxxxxxxxxx.xxxxxxxxxx => xxxxxxxxxx-xxxxxxxxxx` + * @link https://support.google.com/analytics/answer/11397207 + */ +function extractGoogleAnalyticsUserIdFromCookie(gaCookie) { + if (gaCookie) { + // Remove the Google Analytics tracker from the device ID. + const userIdParts = gaCookie.split('.').slice(-2); + if (userIdParts.length === 2) { + return userIdParts.join('-'); + } + } + return undefined; +}; + +function getBrowserCookie(cookieName) { + // In React Native environments, `document.cookie` doesn't exist. + if (typeof document !== 'object' || typeof document.cookie !== 'string') { + return undefined; + } + const name = cookieName + '='; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i].trim(); + if (c.startsWith(name)) { + return c.substring(name.length); + } + } + return undefined; +}; + +function getGoogleAnalyticsUserIdFromBrowserCookie(cookieName) { + const browserCookie = getBrowserCookie(cookieName); + return browserCookie ? extractGoogleAnalyticsUserIdFromCookie(browserCookie) : undefined; +}; + +// make sure the cookie is available when the script first loads, i.e. when the user first visits the website +const ga_cookie_id = getGoogleAnalyticsUserIdFromBrowserCookie('_ga'); + +window.kapaSettings = { user: { uniqueClientId: ga_cookie_id } };