Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MAIN] [STRATCONN] added consent for google campaign manager #2021

Merged
merged 7 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
import { Subscription } from '@segment/browser-destination-runtime/types'
import googleCampaignManager, { destination } from '../index'
import { Analytics, Context } from '@segment/analytics-next'

const subscriptions: Subscription[] = [
{
partnerAction: 'salesActivity',
name: 'Sales Activity',
enabled: true,
subscribe: 'type = "track"',
mapping: {}
}
]

describe('Google Tag for Campaign Manager', () => {
const defaultSettings = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you also pls add a test for enableConsentMode not being set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added.

advertiserId: 'test123',
allowAdPersonalizationSignals: false,
conversionLinker: false
}
beforeEach(async () => {
jest.restoreAllMocks()

const [googleCampaignManagerPlugin] = await googleCampaignManager({
...defaultSettings,
subscriptions
})
jest.spyOn(destination, 'initialize')

await googleCampaignManagerPlugin.load(Context.system(), {} as Analytics)
})

it('should not update consent if enable_consent mode is denied', async () => {
const settings = {
...defaultSettings,
enableConsentMode: false
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([expect.not.objectContaining(Object.assign({}, ['consent', 'default', {}]))])
)
})

it('should update consent if analytics storage is granted', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
defaultAnalyticsStorageConsentState: 'granted'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { analytics_storage: 'granted' }]))
])
)
})

it('should update consent if analytics storage is denied', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
defaultAnalyticsStorageConsentState: 'denied'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { analytics_storage: 'denied' }]))
])
)
})
it('should update consent if Ad storage is granted', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
defaultAdsStorageConsentState: 'granted'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_storage: 'granted' }]))
])
)
})
it('should update consent if Ad storage is denied', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
defaultAdsStorageConsentState: 'denied'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_storage: 'denied' }]))
])
)
})
it('should update consent if Ad user data is granted', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
adUserDataConsentState: 'granted'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_user_data: 'granted' }]))
])
)
})
it('should update consent if Ad user data is denied', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
adUserDataConsentState: 'denied'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_user_data: 'denied' }]))
])
)
})
it('should update consent if Ad personalization is granted', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
adPersonalizationConsentState: 'granted'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_personalization: 'granted' }]))
])
)
})
it('should update consent if Ad personalization is denied', async () => {
const settings = {
...defaultSettings,
enableConsentMode: true,
adPersonalizationConsentState: 'denied'
}

const [event] = await googleCampaignManager({ ...settings, subscriptions })
await event.load(Context.system(), {} as Analytics)
expect(destination.initialize).toHaveBeenCalled()

expect(window.dataLayer).toEqual(
expect.arrayContaining([
expect.objectContaining(Object.assign({}, ['consent', 'default', { ad_personalization: 'denied' }]))
])
)
})
})

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
declare global {
interface Window {
gtag: typeof gtag
dataLayer: any

Check warning on line 10 in packages/browser-destinations/destinations/google-campaign-manager/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Unexpected any. Specify a different type
}
}

type ConsentParamsArg = 'granted' | 'denied' | undefined

export const destination: BrowserDestinationDefinition<Settings, Function> = {
name: 'Google Tag for Campaign Manager',
slug: 'actions-google-campaign-manager',
Expand Down Expand Up @@ -40,6 +42,56 @@
type: 'boolean',
required: true,
default: true
},
enableConsentMode: {
description: `Set to true to enable Google’s [Consent Mode](https://support.google.com/analytics/answer/9976101?hl=en). Set to false by default.`,
label: 'Enable Consent Mode',
type: 'boolean',
default: false
},
adUserDataConsentState: {
description:
'Consent state indicated by the user for ad cookies. Value must be "granted" or "denied." This is only used if the Enable Consent Mode setting is on.',
label: 'Ad User Data Consent State',
type: 'string',
choices: [
{ label: 'Granted', value: 'granted' },
{ label: 'Denied', value: 'denied' }
],
default: undefined
},
adPersonalizationConsentState: {
description:
'Consent state indicated by the user for ad cookies. Value must be "granted" or "denied." This is only used if the Enable Consent Mode setting is on.',
label: 'Ad Personalization Consent State',
type: 'string',
choices: [
{ label: 'Granted', value: 'granted' },
{ label: 'Denied', value: 'denied' }
],
default: undefined
},
defaultAdsStorageConsentState: {
description:
'The default value for ad cookies consent state. This is only used if Enable Consent Mode is on. Set to “granted” if it is not explicitly set. Consent state can be updated for each user in the Set Configuration Fields action.',
label: 'Default Ads Storage Consent State',
type: 'string',
choices: [
{ label: 'Granted', value: 'granted' },
{ label: 'Denied', value: 'denied' }
],
default: undefined
},
defaultAnalyticsStorageConsentState: {
description:
'The default value for analytics cookies consent state. This is only used if Enable Consent Mode is on. Set to “granted” if it is not explicitly set. Consent state can be updated for each user in the Set Configuration Fields action.',
label: 'Default Analytics Storage Consent State',
type: 'string',
choices: [
{ label: 'Granted', value: 'granted' },
{ label: 'Denied', value: 'denied' }
],
default: undefined
}
},

Expand All @@ -47,14 +99,37 @@
window.dataLayer = window.dataLayer || []
window.gtag = function () {
// eslint-disable-next-line prefer-rest-params
window.dataLayer.push(arguments)

Check warning on line 102 in packages/browser-destinations/destinations/google-campaign-manager/src/index.ts

View workflow job for this annotation

GitHub Actions / Lint (18.x)

Unsafe call of an `any` typed value
}

window.gtag('set', 'allow_ad_personalization_signals', settings.allowAdPersonalizationSignals)
window.gtag('js', new Date())
window.gtag('config', settings.advertiserId, {
conversion_linker: settings.conversionLinker
})
if (settings.enableConsentMode) {
const consent: {
ad_storage?: ConsentParamsArg
analytics_storage?: ConsentParamsArg
ad_user_data?: ConsentParamsArg
ad_personalization?: ConsentParamsArg
allow_ad_personalization_signals?: Boolean
} = {}

if (settings.defaultAnalyticsStorageConsentState) {
consent.analytics_storage = settings.defaultAnalyticsStorageConsentState as ConsentParamsArg
}
if (settings.defaultAdsStorageConsentState) {
consent.ad_storage = settings.defaultAdsStorageConsentState as ConsentParamsArg
}
if (settings.adUserDataConsentState) {
consent.ad_user_data = settings.adUserDataConsentState as ConsentParamsArg
}
if (settings.adPersonalizationConsentState) {
consent.ad_personalization = settings.adPersonalizationConsentState as ConsentParamsArg
}

window.gtag('consent', 'default', consent)
}
const script = `https://www.googletagmanager.com/gtag/js?id=${settings.advertiserId}`
await deps.loadScript(script)
return window.gtag
Expand Down
2 changes: 1 addition & 1 deletion packages/browser-destinations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"size-limit": [
{
"path": "dist/web/*/*.js",
"limit": "151 KB"
"limit": "152 KB"
}
]
}
Loading