Analytics
/

Conditional loading

Conditional enabling & disabling of analytic providers


With the analytics library it's possible to start your application & wait for user input before any third party analytics tools are loaded into the page.

This will ensure the users privacy settings respected & follow GDPR guidelines.

Hooray 🎉

How does it work?

All plugins support an enabled option. Additionally any third party plugin supports this option as well.

When the enabled flag is set to false, the plugin will noOp and not load any third party javascript in the users browser.

Setting enabled: false will cause the analytics plugin initialize function to be skipped and it will not perform any of its setup steps (like injected the tracking script on to the page).

Disabling initial loading

The easiest way is setting enabled to false in the plugin config.

import Analytics from 'analytics'
import googleAnalytics from '@analytics/google-analytics'

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    googleAnalytics({
      trackingId: 'ua-111-22222',
      // Disable GA from loading 
      enabled: false,
    }),
  ]
})

// Google Analytics will not load unless
// analytics.plugins.enable('google-analytics') is called

Enabling & Disabling plugins

analytics.plugins API methods allow for conditional enabling/disabling of integrations.

// Enable google analytics based on user input...
analytics.plugins.enable('google-analytics')

// You can also disable plugins dynamically
analytics.plugins.disable(['plugin-1', 'plugin-3'])

Using with Do Not Track

You can combine the enabled flag with the Do not track utility

import Analytics from 'analytics'
import googleAnalytics from '@analytics/google-analytics'
import { doNotTrackEnabled } from 'analytics-plugin-do-not-track'

const dontTrack = doNotTrackEnabled()

const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    googleAnalytics({
      trackingId: 'ua-111-22222',
      enabled: !dontTrack
    })
  ]
})

Full Example

import Analytics from 'analytics'
import googleAnalyticsPlugin from '@analytics/google-analytics'
import hubspotPlugin from '@analytics/hubspot'


const analytics = Analytics({
  app: 'awesome-app',
  plugins: [
    googleAnalyticsPlugin({
      trackingId: 'UA-xyz-123',
      // Disable GA from loading until `analytics.plugins.enable` called 
      enabled: false,
    }),
    hubSpotPlugin({
      portalId: '234576',
      // Disable HubSpot from loading `analytics.plugins.enable` called
      enabled: false,
    })
  ]
})

/* Because no analytic plugin is enabled, all calls will noOp & not load any third party JS */
analytics.page() // NoOp
analytics.track(...) // NoOp
analytics.identify(...) // NoOp

/**
 * Later in your app after getting user permission to load analytics tools...
 */

/* User opted in, enable analytics plugins */
analytics.plugins.enable(['google-analytics', 'hubspot']).then(() => {
  /* Plugins are now loaded into the page */

  /* Track a page view */
  analytics.page()

  /* Track a custom event */
  analytics.track('cartCheckout', {
    item: 'pink socks',
    price: 20
  })

  /* Identify a visitor */
  analytics.identify('userId123', {
    name: 'bob',
    email: 'bob@bob.com'
  })
})

Ref issue