getTimeBetweenEvents for web sdk implemenentation in adobe analytics | Community
Skip to main content
Level 2
October 4, 2024
Question

getTimeBetweenEvents for web sdk implemenentation in adobe analytics

  • October 4, 2024
  • 1 reply
  • 1466 views

getTimeBetweenEvents plug-in is not available for web SDK implementation, is there an alternative to it for web SDK implementation?

 

I need to find the time spent by the users between two events:

A particular use case would be the time it takes for a user to go from adding the first item to the cart to clicking "confirm order."

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.

1 reply

bjoern__koth
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 4, 2024

Hi @firefighteradi 

yeah that's indeed not possible. I guess a couple of lines of code in the might be able to solve that in the "on before event send callback code" though.

How do you pass in analytics events? meaning, do you use data.__adobe.analytics or the "Adobe Analytics ExperienceEvent Full Extension schema field group" (i.e., xdm._experience.analytics...)?

 

 

Top of my head, something like this might work, you would obviously have to take it from there and set the resulting time to which ever prop/eVar you need.

// Modify content.xdm or content.data as necessary. There is no need to wrap the // code in a function or return a value. For example: // content.xdm.web.webPageDetails.name = "Checkout"; const findEventInPayload = (eventName) => { _satellite.logger.debug(">>> findEventInPayload", eventName); if (content?.data?.__adobe?.analytics?.events?.includes?.(eventName)) { // contextdata is present // _satellite.logger.debug(">>> timer start event found in data.__adobe.analytics.events"); return true; } else if (content?.xdm?._experience?.analytics?.event1to100?.[eventName]?.value) { // _satellite.logger.debug(">>> timer start event found in xdm._experience.analytics"); return true; } return false; }; // dummy data // const content = { // xdm: { // _experience: { // analytics: { // event1to100: { // event90: { // value: "timerStart" // } // } // } // } // }, // data: { // __adobe: { // analytics: { // events: "event90", // test start // // events: "event91", // test end // } // } // } // }; // 1. set and persist timer when the event occurs // check if startEvent is present either in xdm._experience.analytics data or in data.__adobe.analytics.events const startEvent = "event90"; const endEvent = "event91"; if (findEventInPayload(startEvent)) { // persist in sessionStorage sessionStorage.setItem("timerStart", new Date().getTime().toString()); } // 2. calculate the time difference when the end event occurs if (findEventInPayload(endEvent)) { const startTimer = sessionStorage.getItem("timerStart"); if (startTimer) { const endTimer = new Date().getTime(); const timeDifference = Math.round((endTimer - parseInt(startTimer)) / 1000); // persist in sessionStorage _satellite.logger.debug(">>> timeDifference", timeDifference.toString()); // do whatever you need want to do with the data i.e., set it on the data.__adobe.analytics.eVarXYZ or so // remove startTimer sessionStorage.removeItem("timerStart"); } }

 

Cheers from Switzerland!
Jennifer_Dungan
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 7, 2024

Yes, I would also say that there is no plugin, so any solution will have to be manually coded.

 

However, the issue with the SessionStorage solution is that it will only work within the same visit... if you are expecting days between events (like add to cart and purchase), then this is going to be a problem... 

 

You could try LocalStorage (only if the user is accepting LocalStorage usage... a lot of private browsing modes turn that off), or cookies (which I know are subject to ITP and the cookie-apocalypse)... but at least these solutions should work across visits.

 

But the same approach applies, you will need to store something (including a timestamp) when the first event is triggered, then when the second is triggered, extract that, and calculate the difference between the current time and the stored time... 

 

If you are using Raw Data exports, you might consider just getting your data engineering team to use SQL to calculate this out... you won't be able to access it inside of Workspace... but it might be less complex....

bjoern__koth
Community Advisor and Adobe Champion
Community Advisor and Adobe Champion
October 7, 2024

Yeah I guess two options. And frankly, I anyway don't really like that getTimeBetweenEvents is working with cookies and cannot be configured to use session or local storage.

 

The code base should at least give you an idea how it can be tackled, the rest is up to you ☺️

Cheers from Switzerland!