Skip to content

Application Insights

This module provides a plugin to use and initialize application insights within your vue application.

Dependencies

initApplicationInsights

Before application insights can be used it needs to be initialized by calling this function with. The following options are available to configure the initialization:

OptionTypeDescription
connectionStringstringApplication Insights connection string
enabledbooleanEnable Application Insights after initialization, defaults to false
routerRouterThe router to use for automatic page tracking.
configIConfig & IConfiguration;Application Insights configuration overrides.

INFO

Note that enabled is by default false, this is to prevent accidental logging of data to application insights in regards of the GDPR rules.

In order to use application insights, you first need to call the initApplicationInsights function with a valid connectionString.

The initApplicationInsights function also has an optional config parameter that can be used to extend or override the default configuration.

When the initialization takes place depends on your application needs and cookie policies that apply. We identified 3 different scenarios:

enableApplicationInsights

This function can be used to enable application insights after initialization. This can be useful when you want to enable application insights after the user has given consent to use cookies. Depending where you trigger this, you can choose to track the current page through the trackCurrentPageView parameter. This defaults to true when not provided.

Implementation examples

This is the easiest way to try out application insights. As you don't need any config setup, you can just call initApplicationInsights with the connection string straight in your code. This does however doesn't make it possible to change the connection string between environments.

ts
// App.vue
import { initApplicationInsights } from "@xerius/codey-core/modules/application-insights";

initApplicationInsights({
  connectionString: "your-connection-string",
  enabled: true,
  router,
});

When there are no cookie policies and you just need the config for your aiConnectionString, you can get this from the configStore provided by your application.

ts
// App.vue
import { storeToRefs } from "pinia";
import { initApplicationInsights } from "@xerius/codey-core/modules/application-insights";
import { useConfigStore } from "./store/config.store";

const configStore = useConfigStore();
const { config } = storeToRefs(configStore);
configStore.$subscribe((mutation, state) => {
  if (state.config?.aiConnectionString) {
    initApplicationInsights({
      connectionString: state.config.aiConnectionString,
      enabled: true,
      router,
    });
  }
});

The initialization happens like in the case above, but here we set enabled: false or omit this parameter. This will prevent application insights from tracking any data until it is enabled. Within the cookiebot setup, call the enableApplicationInsights within the onStatisticsAccept callback.

ts
// libs/cookiebot.ts
import { enableApplicationInsights } from "@xerius/codey-core/modules/application-insights";

setupCookiebot({
  // ...
  onStatisticsAccept() {
    enableApplicationInsights();
  },
});

useApplicationInsights

useApplicationInsights returns the instance of the application insights client. This can be used to manually track events, page views, etc. This is not a ref but because the init takes place in the App.vue it will always be initialized before other components use it.