Skip to content

Commit

Permalink
ZRZ-1318 fix engagement time
Browse files Browse the repository at this point in the history
  • Loading branch information
ad-astra-via committed Jun 17, 2024
1 parent 01d6ea7 commit 365f784
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
7 changes: 1 addition & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,7 @@ export const sendEvent = async (
) => {
const { client } = event
const { finalURL, requestBody } = getFinalURL(eventType, event, settings)
console.log(
'πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ final URL is here and send event fires',
finalURL
)
console.log('πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€πŸš€ also manager.fetch is working: ', manager.fetch)

manager.fetch(finalURL, {
headers: { 'User-Agent': client.userAgent },
})
Expand Down Expand Up @@ -109,7 +105,6 @@ export default async function (manager: Manager, settings: ComponentSettings) {
)

manager.addEventListener('pageview', event => {
// this line does not trigger visibilityChange after a pagview, it will start triggering events only on the fist change to hidden
event.client.attachEvent('visibilityChange')

// if engagement duration is >1 send a user_engagement event before pageview, to count the time on previous page properly
Expand Down
15 changes: 4 additions & 11 deletions src/requestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ const getRandomInt = () => Math.floor(2147483647 * Math.random())
const firstPageEvent = (client: Client) => {
const countedEvent = client.get('countedEvent')
if (countedEvent) {
console.log('πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘ this is not the first event!')
return false
} else {
console.log('πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘πŸ₯‘ this is the FIRST event!')
return true
}
}
Expand Down Expand Up @@ -125,22 +123,17 @@ function getToolRequest(
firstPageEvent(client)
) {
requestBody._et = engagementDuration
console.log(
'πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘πŸ’‘ event/ecommerce includes _et: ',
requestBody._et
)

// mark first event to avoid sending _et with upcoming events on that page
event.client.set('countedEvent', '1', { scope: 'page' })
// Reset engagementDuration after event has been dispatched so it does not accumulate
// Reset engagementDuration and engagementStart after event has been dispatched so it does not accumulate
event.client.set('engagementDuration', '0')
const now = new Date(Date.now()).getTime()
event.client.set('engagementStart', `${now}`)
} else if (eventType === 'user_engagement') {
requestBody._et = engagementDuration
// Reset engagementDuration after event has been dispatched so it does not accumulate
event.client.set('engagementDuration', '0')
console.log(
'🐝🐝🐝🐝🐝🐝🐝 user_engagement includes _et: ',
requestBody._et
)
}

/* Start of gclid treating */
Expand Down

0 comments on commit 365f784

Please sign in to comment.