/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ const { Console } = require('console'); const path = require('path'); module.exports = function (context) { const { siteConfig } = context; const { themeConfig } = siteConfig; const { appInsights } = themeConfig || {}; if (!appInsights) { throw new Error( `You need to specify "appInsights" object in "themeConfig" with "instrumentationKey" field in it to use appinsights.`, ); } const { instrumentationKey } = appInsights; if (!instrumentationKey) { throw new Error( 'You specified the "appInsights" object in "themeConfig" but the "trackingID" field was missing. ' + 'Please ensure this is not a mistake.', ); } const isProd = process.env.NODE_ENV !== 'development'; return { name: 'docusaurus-plugin-app-insights', getClientModules() { return isProd ? [path.resolve(__dirname, './analytics')] : []; }, injectHtmlTags() { if (!isProd) { return {}; } return { headTags: [ { tagName: 'script', attributes: { type: 'text/javascript' }, innerHTML: ` !function(T,l,y){var S=T.location,k="script",D="instrumentationKey",C="ingestionendpoint",I="disableExceptionTracking",E="ai.device.",b="toLowerCase",w="crossOrigin",N="POST",e="appInsightsSDK",t=y.name||"appInsights";(y.name||T[e])&&(T[e]=t);var n=T[t]||function(d){var g=!1,f=!1,m={initialize:!0,queue:[],sv:"5",version:2,config:d};function v(e,t){var n={},a="Browser";return n[E+"id"]=a[b](),n[E+"type"]=a,n["ai.operation.name"]=S&&S.pathname||"_unknown_",n["ai.internal.sdkVersion"]="javascript:snippet_"+(m.sv||m.version),{time:function(){var e=new Date;function t(e){var t=""+e;return 1===t.length&&(t="0"+t),t}return e.getUTCFullYear()+"-"+t(1+e.getUTCMonth())+"-"+t(e.getUTCDate())+"T"+t(e.getUTCHours())+":"+t(e.getUTCMinutes())+":"+t(e.getUTCSeconds())+"."+((e.getUTCMilliseconds()/1e3).toFixed(3)+"").slice(2,5)+"Z"}(),iKey:e,name:"Microsoft.ApplicationInsights."+e.replace(/-/g,"")+"."+t,sampleRate:100,tags:n,data:{baseData:{ver:2}}}}var h=d.url||y.src;if(h){function a(e){var t,n,a,i,r,o,s,c,u,p,l;g=!0,m.queue=[],f||(f=!0,t=h,s=function(){var e={},t=d.connectionString;if(t)for(var n=t.split(";"),a=0;a