Skip to content

Commit

Permalink
ZRZ-1324 make seg true when session > 10sec + include two pageviews a…
Browse files Browse the repository at this point in the history
…nd a conversion event
  • Loading branch information
ad-astra-via committed May 31, 2024
1 parent d0a4518 commit 6c1ed85
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
15 changes: 14 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { ComponentSettings, Manager, MCEvent } from '@managed-components/types'
import {
Client,

Check failure on line 2 in src/index.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

'Client' is defined but never used
ComponentSettings,
Manager,
MCEvent,
} from '@managed-components/types'
import { getFinalURL } from './requestBuilder'
import { countConversion, countPageview } from './utils'

const SESSION_DURATION_IN_MIN = 30

Expand Down Expand Up @@ -137,6 +143,8 @@ export default async function (manager: Manager, settings: ComponentSettings) {
manager.createEventListener('visibilityChange', onVisibilityChange(settings))

manager.addEventListener('event', event => {
// count conversion events for 'seg' value
countConversion(event)
// order matters so engagement duration is set before dispatching the hit
computeEngagementDuration(event)

Expand All @@ -149,7 +157,10 @@ export default async function (manager: Manager, settings: ComponentSettings) {
manager.addEventListener('pageview', event => {
event.client.attachEvent('visibilityChange')

// count pageviews for 'seg' value
countPageview(event.client)
// order matters so engagement duration is set before dispatching the hit

computeEngagementDuration(event)

sendEvent('page_view', event, settings)
Expand All @@ -159,6 +170,8 @@ export default async function (manager: Manager, settings: ComponentSettings) {
})

manager.addEventListener('ecommerce', async event => {
// count conversion events for 'seg' value
countConversion(event)
// order matters so engagement duration is set before dispatching the hit
computeEngagementDuration(event)

Expand Down
27 changes: 22 additions & 5 deletions src/requestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,30 @@ function getToolRequest(
if (!Number.isInteger(sessionCounter)) {
sessionCounter = 0
}
if (typeof client !== 'undefined' && client) {
// Determine if the session is engaged to set the 'seg' value
const pageviewCounter = parseInt(client.get('pageviewCounter') || '0')
const conversionCounter = parseInt(client.get('conversionCounter') || '0')
let engagementDuration = parseInt(client.get('engagementDuration') || '0')

Check failure on line 68 in src/requestBuilder.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

'engagementDuration' is never reassigned. Use 'const' instead

// Session will be marked engaged if longer than 10 seconds, has at least 1 conversion event, and 2 or more pageviews
if (
engagementDuration > 10 &&
conversionCounter > 0 &&
pageviewCounter > 1
) {
requestBody['seg'] = 1 // Session engaged
} else {
requestBody['seg'] = 0
}
} else {
console.error('Client object is undefined or not initialized')
}

// Create, refresh or renew session id
const sessionLength = 30 * 60 * 1000 // By default, GA4 keeps sessions for 30 minutes
let currentSessionID = client.get('ga4sid')
if (currentSessionID) {
requestBody['seg'] = 1 // Session engaged
} else {
requestBody['seg'] = 0
if (!currentSessionID) {
requestBody['_ss'] = 1 // Session start
sessionCounter++
currentSessionID = getRandomInt().toString()
Expand All @@ -92,7 +108,8 @@ function getToolRequest(
requestBody['cid'] = cid

//const notTheFirstSession = parseInt(requestBody['_s'] as string) > 1
const engagementDuration = parseInt(String(client.get('engagementDuration')), 10) || 0;
const engagementDuration =
parseInt(String(client.get('engagementDuration')), 10) || 0
if (engagementDuration) {
requestBody._et = engagementDuration
}
Expand Down
27 changes: 27 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Client, MCEvent } from "@managed-components/types"

Check failure on line 1 in src/utils.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Replace `"@managed-components/types"` with `'@managed-components/types'`

export const flattenKeys = (obj: { [k: string]: unknown } = {}, prefix = '') =>
Object.keys(obj).reduce((acc: { [k: string]: unknown }, k) => {
const pre = prefix.length ? `${prefix}.` : ''
Expand Down Expand Up @@ -43,3 +45,28 @@ export const getParamSafely = (
}
return {}
}
// pageviews in session counter
export const countPageview = (client: Client) => {
let pageviewCounter = parseInt(client.get('pageviewCounter') || '0') || 0

if (pageviewCounter === 0) {
client.set('pageviewCounter', '1', { scope: 'session' })
} else {
pageviewCounter++
client.set('pageviewCounter', `${pageviewCounter}`, { scope: 'session' })
}
}

// conversion events in session counter
export const countConversion = (event: MCEvent) => {
const {client} = event

Check failure on line 62 in src/utils.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Replace `client` with `·client·`
let conversionCounter = parseInt(client.get('conversionCounter') || '0') || 0
if (conversionCounter === 0 && event.payload.conversion) {
client.set('conversionCounter', '1', { scope: 'session' })
} else {
conversionCounter++
client.set('conversionCounter', `${conversionCounter}`, {
scope: 'session',
})
}
}

Check failure on line 72 in src/utils.ts

View workflow job for this annotation

GitHub Actions / build-test (20.x)

Insert `⏎`

0 comments on commit 6c1ed85

Please sign in to comment.