Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Nov 13, 2023
1 parent ca15a75 commit 6cf7845
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
data-domain-script="09b87b90-1b30-4d68-9610-9d2fe11234f3-test"></script>
<script type="text/javascript">
const wk = document.querySelector("input").value
wk && console.log('should be on the following url ->', `${window.location.origin + window.location.pathname}?writeKey=${wk}&otpreview=true&otgeo=za`)
console.log('should be on the following url ->', `${window.location.origin + window.location.pathname}?writeKey=${wk || '<writekey>'}&otpreview=true&otgeo=za`)
function OptanonWrapper() {
// debugging.
if (!window.OnetrustActiveGroups.replaceAll(',', '')) {
Expand Down Expand Up @@ -147,7 +147,7 @@ <h2>Logs</h2>

<script type="text/javascript">
const displayConsentLogs = (str) => document.querySelector('#consent-changed').textContent = str
analytics.on('track', (name, properties, options) => {
window.analytics?.on('track', (name, properties, options) => {
if (name.includes("Segment Consent")) {
displayConsentLogs("Consent Changed Event Fired")
setTimeout(() => displayConsentLogs(''), 3000)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as ConsentStamping from '../consent-stamping'
import * as ConsentChanged from '../analytics/consent-changed'
import { createWrapper } from '../create-wrapper'
import { AbortLoadError, LoadContext } from '../load-cancellation'
import type {
Expand All @@ -11,6 +10,7 @@ import type {
} from '../../types'
import { CDNSettingsBuilder } from '@internal/test-helpers'
import { assertIntegrationsContainOnly } from './assertions/integrations-assertions'
import { AnalyticsService } from '../analytics'

const DEFAULT_LOAD_SETTINGS = {
writeKey: 'foo',
Expand Down Expand Up @@ -661,8 +661,8 @@ describe(createWrapper, () => {

describe('registerOnConsentChanged', () => {
const sendConsentChangedEventSpy = jest.spyOn(
ConsentChanged,
'sendConsentChangedEvent'
AnalyticsService.prototype,
'consentChange'
)

let categoriesChangedCb: (categories: Categories) => void = () => {
Expand Down Expand Up @@ -709,8 +709,6 @@ describe(createWrapper, () => {
expect(consoleErrorSpy).toBeCalledTimes(1)
const err = consoleErrorSpy.mock.lastCall[0]
expect(err.toString()).toMatch(/validation/i)
// if OnConsentChanged callback is called with categories, it should send event
expect(sendConsentChangedEventSpy).not.toBeCalled()
expect(analyticsTrackSpy).not.toBeCalled()
})
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { AnyAnalytics, Categories, CDNSettings } from '../../types'
import { createConsentStampingMiddleware } from '../consent-stamping'
import { validateAnalyticsInstance } from '../validation'
import { validateAndSendConsentChangedEvent } from './consent-changed'
import { validateAnalyticsInstance, validateCategories } from '../validation'
import { getInitializedAnalytics } from './get-initialized-analytics'

/**
* This class is a wrapper around the analytics.js library.
*/
export class AnalyticsService {
/**
* The original analytics.load fn
Expand Down Expand Up @@ -45,7 +47,32 @@ export class AnalyticsService {
return this.analytics.addSourceMiddleware(MW)
}

/**
* Dispatch an event that looks like:
* ```ts
* {
* "type": "track",
* "event": "Segment Consent Preference",
* "context": {
* "consent": {
* "categoryPreferences" : {
* "C0001": true,
* "C0002": false,
* }
* }
* ...
* ```
*/
consentChange(categories: Categories): void {
return validateAndSendConsentChangedEvent(this.analytics, categories)
try {
validateCategories(categories)
} catch (e: unknown) {
// not sure if there's a better way to handle this
return console.error(e)
}
const CONSENT_CHANGED_EVENT = 'Segment Consent Preference'
this.analytics.track(CONSENT_CHANGED_EVENT, undefined, {
consent: { categoryPreferences: categories },
})
}
}

This file was deleted.

0 comments on commit 6cf7845

Please sign in to comment.