2.0.1 • Published 18 days ago

@segment/analytics-consent-tools v2.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
18 days ago

@segment/analytics-consent-tools

Quick Start

// wrapper.js
import { createWrapper, resolveWhen } from '@segment/analytics-consent-tools'

export const withCMP = createWrapper({
  // Wait to load wrapper or call "shouldLoadSegment" until window.CMP exists.
  shouldLoadWrapper: async () => {
    await resolveWhen(() => window.CMP !== undefined, 500)
  },

  // Allow for control over wrapper + analytics initialization.
  // Delay any calls to analytics.load() until this function returns / resolves.
  shouldLoadSegment: async (ctx) => {
    /*
    // Optional -- for granular control of initialization
    if (noConsentNeeded) {
      ctx.abort({ loadSegmentNormally: true })
    } else if (allTrackingDisabled) {
      ctx.abort({ loadSegmentNormally: false })
    }
    */
    if (window.CMP.ConsentModel === 'opt-out') {
      return ctx.load({ consentModel: 'opt-out' })
    } else {
      await resolveWhen(
        () => !window.CMP.popUpVisible() && window.CMP.categories.length,
        500
      )
      return ctx.load({ consentModel: 'opt-in' })
    }
  },

  getCategories: () => {
    return normalizeCategories(window.CMP.consentedCategories()) // Expected format: { foo: true, bar: false }
  },

  registerOnConsentChanged: (setCategories) => {
    window.CMP.onConsentChanged((event) => {
      setCategories(normalizeCategories(event.detail))
    })
  },
})

Settings / Configuration

See: settings.ts

Consent Models

The wrapper has different behavior based on the consent-model:

  • opt-in - (strict, GDPR scenario) -- Unconsented device mode destinations are removed
  • opt-out - Device mode destinations are loaded, but with blocking middleware

Wrapper Usage API

npm

import { withCMP } from './wrapper'
import { AnalyticsBrowser } from '@segment/analytics-next'

export const analytics = new AnalyticsBrowser()

withCMP(analytics).load({
  writeKey: '<MY_WRITE_KEY'>
})

Snippet users (window.analytics)

Note: This assumes a project that can consume the library via es6 imports, using a like Webpack.

  1. Delete the analytics.load() line from the snippet
- analytics.load("<MY_WRITE_KEY>");
  1. Import Analytics
import { withCMP } from './wrapper'

withCMP(window.analytics).load('<MY_WRITE_KEY')

Wrapper Examples

Settings / Options / Configuration

See the complete list of settings in the Settings interface

Special Requirements

  • For npm users, this library expects a version of @segment/analytics-next >= 1.53.1. Note: If your library depends on this library, you should have the appropriate peer dependency declaration. See our package.json for an example.

Development

  1. Build this package + all dependencies
yarn . build
  1. Run tests
yarn test
2.0.1

18 days ago

2.0.0

21 days ago

1.2.0

4 months ago

1.1.0

5 months ago

1.0.0

6 months ago

0.2.1

7 months ago

0.2.0

8 months ago

0.1.1

9 months ago

0.1.0

9 months ago

0.0.4

9 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago