Skip to content

Commit

Permalink
Merge pull request #5 from managed-components/ZRZ-1078
Browse files Browse the repository at this point in the history
Zrz 1078
  • Loading branch information
ad-astra-via authored Apr 24, 2024
2 parents 13ccb74 + 6e47bd6 commit cdf94eb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
3 changes: 1 addition & 2 deletions src/conversionLinker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ComponentSettings, MCEvent } from '@managed-components/types'

export const conversionLinkerHandler = async (
eventType: string,
export const conversionLinkerHandler = (
event: MCEvent,
settings: ComponentSettings
) => {
Expand Down
33 changes: 22 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ export const eventHandler = async (
// bg: "ffffff",
// u_nplug: 3
// u_nmime: 4

if (settings.conversionLinker) {
// only handle conversion linker if gclid query param exists
if (client.url.searchParams.get('gclid')) {
conversionLinkerHandler(event, settings)
}
}

if (client.url.searchParams.get('_gl')) {
try {
const gclaw = atob(
Expand All @@ -86,12 +94,14 @@ export const eventHandler = async (
console.error(e)
}
}
if (eventType === 'pageview') {
return
}

if (client.get('_gcl_aw')) {
query.gclaw = client.get('_gcl_aw')?.split('.').pop()
}
if (client.get('gclid')) {
query.gclaw = client.get('gclid')
}

if (query.gclaw) {
const url = new URL(query.url)
url.searchParams.append('gclid', query.gclaw)
Expand All @@ -106,10 +116,12 @@ export const eventHandler = async (

const params = new URLSearchParams({ ...query, ...payload }).toString()

const baseURL =
eventType === 'remarketing'
? 'https://www.google.com/pagead/1p-user-list'
: 'https://www.googleadservices.com/pagead/conversion'
let baseURL = ''
if (eventType === 'remarketing') {
baseURL = 'https://www.google.com/pagead/1p-user-list'
} else {
baseURL = 'https://www.googleadservices.com/pagead/conversion'
}
neededFetch.push(`${baseURL}/${conversionId}/?${params}`)

neededFetch.push(
Expand All @@ -123,13 +135,12 @@ export const eventHandler = async (
keepalive: true,
})
})

if (settings.conversionLinker) {
await conversionLinkerHandler(eventType, event, settings)
}
}

export default async function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', event => {
eventHandler('pageview', event, settings)
})
manager.addEventListener('conversion', event => {
eventHandler('conversion', event, settings)
})
Expand Down

0 comments on commit cdf94eb

Please sign in to comment.