Skip to content

Commit

Permalink
[GA4 Web] Page View defaults (segmentio#1922)
Browse files Browse the repository at this point in the history
* Update with suggestion

* Add test case

* Update tests + description

* fix failing test

* fixes test descriptions
  • Loading branch information
maryamsharif authored Mar 12, 2024
1 parent 8dcb29f commit d4a3f09
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ describe('Set Configuration Fields action', () => {
})
})

it('should update config if payload has send_page_view is true', async () => {
it('pageView is true and send_page_view is true -> nothing', async () => {
const settings = {
...defaultSettings,
pageView: true
Expand All @@ -491,7 +491,64 @@ describe('Set Configuration Fields action', () => {
const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {}
properties: {
send_page_view: true
}
})

setConfigurationEvent.page?.(context)
expect(mockGtag).toHaveBeenCalledWith('config', 'G-XXXXXXXXXX', {
allow_ad_personalization_signals: false,
allow_google_signals: false
})
})
it('pageView is true and send_page_view is false -> false', async () => {
const settings = {
...defaultSettings,
pageView: true
}

const [setConfigurationEventPlugin] = await googleAnalytics4Web({
...settings,
subscriptions
})
setConfigurationEvent = setConfigurationEventPlugin
await setConfigurationEventPlugin.load(Context.system(), {} as Analytics)

const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {
send_page_view: false
}
})

setConfigurationEvent.page?.(context)
expect(mockGtag).toHaveBeenCalledWith('config', 'G-XXXXXXXXXX', {
allow_ad_personalization_signals: false,
allow_google_signals: false,
send_page_view: false
})
})
it('pageView is true and send_page_view is undefined -> true', async () => {
const settings = {
...defaultSettings,
pageView: true
}

const [setConfigurationEventPlugin] = await googleAnalytics4Web({
...settings,
subscriptions
})
setConfigurationEvent = setConfigurationEventPlugin
await setConfigurationEventPlugin.load(Context.system(), {} as Analytics)

const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {
send_page_view: undefined
}
})

setConfigurationEvent.page?.(context)
Expand All @@ -501,7 +558,8 @@ describe('Set Configuration Fields action', () => {
send_page_view: true
})
})
it('should update config if payload has send_page_view is false', async () => {

it('pageView is false and send_page_view is true -> true', async () => {
const settings = {
...defaultSettings,
pageView: false
Expand All @@ -517,7 +575,9 @@ describe('Set Configuration Fields action', () => {
const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {}
properties: {
send_page_view: true
}
})

setConfigurationEvent.page?.(context)
Expand All @@ -527,7 +587,66 @@ describe('Set Configuration Fields action', () => {
send_page_view: true
})
})
it('should update config if payload has send_page_view is undefined', async () => {

it('pageView is false and send_page_view is false -> false', async () => {
const settings = {
...defaultSettings,
pageView: false
}

const [setConfigurationEventPlugin] = await googleAnalytics4Web({
...settings,
subscriptions
})
setConfigurationEvent = setConfigurationEventPlugin
await setConfigurationEventPlugin.load(Context.system(), {} as Analytics)

const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {
send_page_view: false
}
})

setConfigurationEvent.page?.(context)
expect(mockGtag).toHaveBeenCalledWith('config', 'G-XXXXXXXXXX', {
allow_ad_personalization_signals: false,
allow_google_signals: false,
send_page_view: false
})
})

it('pageView is false and send_page_view is undefined -> false', async () => {
const settings = {
...defaultSettings,
pageView: false
}

const [setConfigurationEventPlugin] = await googleAnalytics4Web({
...settings,
subscriptions
})
setConfigurationEvent = setConfigurationEventPlugin
await setConfigurationEventPlugin.load(Context.system(), {} as Analytics)

const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {
send_page_view: undefined
}
})

setConfigurationEvent.page?.(context)
expect(mockGtag).toHaveBeenCalledWith('config', 'G-XXXXXXXXXX', {
allow_ad_personalization_signals: false,
allow_google_signals: false,
send_page_view: false
})
})

it('pageView is undefined and send_page_view is undefined -> true', async () => {
const settings = {
...defaultSettings,
pageView: undefined
Expand All @@ -543,7 +662,9 @@ describe('Set Configuration Fields action', () => {
const context = new Context({
event: 'setConfigurationFields',
type: 'page',
properties: {}
properties: {
send_page_view: undefined
}
})

setConfigurationEvent.page?.(context)
Expand Down

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 @@ -117,7 +117,7 @@ const action: BrowserActionDefinition<Settings, Function, Payload> = {
type: 'string'
},
send_page_view: {
description: 'Set to false to prevent sending a page_view.',
description: 'Selection overrides toggled value set within Settings',
label: 'Send Page Views',
type: 'boolean',
choices: [
Expand Down Expand Up @@ -176,12 +176,9 @@ const action: BrowserActionDefinition<Settings, Function, Payload> = {
if (checkCookiePathDefaultValue) {
config.cookie_path = settings.cookiePath
}
if (settings.pageView != true) {
config.send_page_view = settings.pageView ?? true
}

if (payload.send_page_view != true) {
config.send_page_view = payload.send_page_view ?? true
if (payload.send_page_view != true || settings.pageView != true) {
config.send_page_view = payload.send_page_view ?? settings.pageView ?? true
}
if (settings.cookieFlags) {
config.cookie_flags = settings.cookieFlags
Expand Down

0 comments on commit d4a3f09

Please sign in to comment.