UNPKG

@microsoft/docusaurus-plugin-application-insights

Version:
47 lines (46 loc) 1.72 kB
// Copyright (c) Microsoft Corporation. // Licensed under the MIT license. /// <reference lib="dom" /> import { ClickAnalyticsPlugin } from '@microsoft/applicationinsights-clickanalytics-js'; import { ApplicationInsights } from '@microsoft/applicationinsights-web'; let appInsights; if (typeof window !== 'undefined') { const pluginConfig = window.appInsightsPluginConfig; if (pluginConfig) { if (pluginConfig.enableClickAnalytics) { const clickPluginInstance = new ClickAnalyticsPlugin(); pluginConfig.config = { ...pluginConfig.config, extensions: [clickPluginInstance], extensionConfig: { [clickPluginInstance.identifier]: { autoCapture: true, dataTags: { useDefaultContentNameOrId: true, }, urlCollectQuery: true, }, }, }; } appInsights = new ApplicationInsights({ config: pluginConfig.config, }); appInsights.loadAppInsights(); appInsights.trackPageView(); } } const clientModule = { onRouteDidUpdate({ location, previousLocation }) { if (previousLocation && (location.pathname !== previousLocation.pathname || location.search !== previousLocation.search || location.hash !== previousLocation.hash)) { // don't log hash, leave for client side data appInsights?.trackPageView({ name: location.pathname + location.search, }); } }, }; export default clientModule;