Skip to content

Commit

Permalink
STRATCONN-3095: Develop new Google Campaign Manager Action Destination (
Browse files Browse the repository at this point in the history
#1555)

* initial scaffolding

* draft

* fix build

* scaffold actions

* scaffold counter mappings

* naive counter action implementation

* naive sales action implementation

* add unit tests for counter activity

* add unit tests for sales activity

* fixes discovered during local testing

* fix labels

* update generated type

* register destination

* add default subscription

* remove TODO

* remove TODO

* Apply suggestions from code review

Co-authored-by: Marín Alcaraz <[email protected]>

* address PR comments

* undo typed gtag to reduce blast radius

---------

Co-authored-by: Marín Alcaraz <[email protected]>
  • Loading branch information
rhall-twilio and marinhero authored Sep 8, 2023
1 parent dbf01b3 commit 70d4a8c
Show file tree
Hide file tree
Showing 14 changed files with 651 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# @segment/analytics-browser-actions-google-campaign-manager

The Google Campaign Manager browser action destination for use with @segment/analytics-next.

## License

MIT License

Copyright (c) 2023 Segment

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

## Contributing

All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@segment/analytics-browser-actions-google-campaign-manager",
"version": "1.0.0",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/segmentio/action-destinations",
"directory": "packages/browser-destinations/destinations/google-campaign-manager"
},
"main": "./dist/cjs",
"module": "./dist/esm",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
"build:cjs": "tsc --module commonjs --outDir ./dist/cjs",
"build:esm": "tsc --outDir ./dist/esm"
},
"typings": "./dist/esm",
"dependencies": {
"@segment/browser-destination-runtime": "^1.4.0"
},
"peerDependencies": {
"@segment/analytics-next": "*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import { Subscription } from '@segment/browser-destination-runtime/types'
import { Analytics, Context } from '@segment/analytics-next'
import googleCampaignManager, { destination } from '../index'
import { GTAG } from '../types'

const subscriptions: Subscription[] = [
{
partnerAction: 'counterActivity',
name: 'Counter Activity',
enabled: true,
subscribe: 'type = "track"',
mapping: {
activityGroupTagString: {
'@path': '$.properties.activityGroupTagString'
},
activityTagString: {
'@path': '$.properties.activityTagString'
},
countingMethod: {
'@path': '$.properties.countingMethod'
},
enableDynamicTags: {
'@path': '$.properties.enableDynamicTags'
},
sessionId: {
'@path': '$.properties.sessionId'
},
uVariables: {
u1: 'custom variable 1',
u2: 'custom variable 2'
},
dcCustomParams: {
dc_lat: 0,
tag_for_child_directed_treatment: 1
}
}
}
]

describe('GoogleCampaignManager.counterActivity', () => {
const settings = {
advertiserId: 'test123',
allowAdPersonalizationSignals: false,
conversionLinker: false
}

let mockGTAG: GTAG
let counterActivityEvent: any
beforeEach(async () => {
jest.restoreAllMocks()

const [trackEventPlugin] = await googleCampaignManager({
...settings,
subscriptions
})
counterActivityEvent = trackEventPlugin

jest.spyOn(destination, 'initialize').mockImplementation(() => {
mockGTAG = {
gtag: jest.fn()
}
return Promise.resolve(mockGTAG.gtag)
})
await trackEventPlugin.load(Context.system(), {} as Analytics)
})

test('track event', async () => {
const activityGroupTagString = 'group'
const activityTagString = 'activity'
const countingMethod = 'standard'
const enableDynamicTags = false

const context = new Context({
event: 'Counter Activity',
type: 'track',
properties: {
activityGroupTagString,
activityTagString,
countingMethod,
enableDynamicTags
}
})
await counterActivityEvent.track?.(context)

expect(mockGTAG.gtag).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining('conversion'),
expect.objectContaining({
allow_custom_scripts: enableDynamicTags,
send_to: `${settings.advertiserId}/${activityGroupTagString}/${activityTagString}+${countingMethod}`,
u1: 'custom variable 1',
u2: 'custom variable 2',
dc_custom_params: { dc_lat: 0, tag_for_child_directed_treatment: 1 }
})
)
})

test('track event (per session)', async () => {
const activityGroupTagString = 'group'
const activityTagString = 'activity'
const countingMethod = 'per_session'
const enableDynamicTags = false
const sessionId = 'my_session'

const context = new Context({
event: 'Counter Activity',
type: 'track',
properties: {
activityGroupTagString,
activityTagString,
countingMethod,
enableDynamicTags,
sessionId
}
})
await counterActivityEvent.track?.(context)

expect(mockGTAG.gtag).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining('conversion'),
expect.objectContaining({
allow_custom_scripts: enableDynamicTags,
send_to: `${settings.advertiserId}/${activityGroupTagString}/${activityTagString}+${countingMethod}`,
session_id: sessionId,
u1: 'custom variable 1',
u2: 'custom variable 2',
dc_custom_params: { dc_lat: 0, tag_for_child_directed_treatment: 1 }
})
)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import { Subscription } from '@segment/browser-destination-runtime/types'
import { Analytics, Context } from '@segment/analytics-next'
import googleCampaignManager, { destination } from '../index'
import { GTAG } from '../types'

const subscriptions: Subscription[] = [
{
partnerAction: 'salesActivity',
name: 'Sales Activity',
enabled: true,
subscribe: 'type = "track"',
mapping: {
activityGroupTagString: {
'@path': '$.properties.activityGroupTagString'
},
activityTagString: {
'@path': '$.properties.activityTagString'
},
countingMethod: {
'@path': '$.properties.countingMethod'
},
enableDynamicTags: {
'@path': '$.properties.enableDynamicTags'
},
value: {
'@path': '$.properties.value'
},
transactionId: {
'@path': '$.properties.transactionId'
},
quantity: 1,
uVariables: {
u1: 'custom variable 1',
u2: 'custom variable 2'
},
dcCustomParams: {
dc_lat: 0,
tag_for_child_directed_treatment: 1
}
}
}
]

describe('GoogleCampaignManager.salesActivity', () => {
const settings = {
advertiserId: 'test123',
allowAdPersonalizationSignals: false,
conversionLinker: false
}

let mockGTAG: GTAG
let salesActivityEvent: any
beforeEach(async () => {
jest.restoreAllMocks()

const [trackEventPlugin] = await googleCampaignManager({
...settings,
subscriptions
})
salesActivityEvent = trackEventPlugin

jest.spyOn(destination, 'initialize').mockImplementation(() => {
mockGTAG = {
gtag: jest.fn()
}
return Promise.resolve(mockGTAG.gtag)
})
await trackEventPlugin.load(Context.system(), {} as Analytics)
})

test('track event', async () => {
const activityGroupTagString = 'group'
const activityTagString = 'activity'
const countingMethod = 'transactions'
const enableDynamicTags = false
const transactionId = 'my-transaction'

const context = new Context({
event: 'Sales Activity',
type: 'track',
properties: {
activityGroupTagString,
activityTagString,
countingMethod,
enableDynamicTags,
value: 10,
transactionId
}
})
await salesActivityEvent.track?.(context)

expect(mockGTAG.gtag).toHaveBeenCalledWith(
expect.anything(),
expect.stringContaining('purchase'),
expect.objectContaining({
allow_custom_scripts: enableDynamicTags,
send_to: `${settings.advertiserId}/${activityGroupTagString}/${activityTagString}+${countingMethod}`,
quantity: 1,
value: 10,
transaction_id: transactionId,
u1: 'custom variable 1',
u2: 'custom variable 2',
dc_custom_params: { dc_lat: 0, tag_for_child_directed_treatment: 1 }
})
)
})
})

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

Loading

0 comments on commit 70d4a8c

Please sign in to comment.