Expand my Community achievements bar.

getTimeBetweenEvents for web sdk implemenentation in adobe analytics

Avatar

Level 2

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."

7 Replies

Avatar

Community Advisor

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...)?

 

bjoern__koth_1-1728047555006.png

 

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!


Avatar

Community Advisor and Adobe Champion

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....

Avatar

Community Advisor

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!


Avatar

Community Advisor and Adobe Champion

Yeah, all the solutions have their drawbacks... when that plugin was originally created, cookies weren't on the warpath... and technically, cookies are the only solution that will cross subdomains I believe...

 

Cookies can be set to the full domain (i.e. www.domain.com) or they can be set to just the main domain name (i.e. .domain.com) 

 

The second option allows the cookie to be shared on any subdomain variation... www. or sub1. or sub2. etc

 

Session Storage and Local Storage I believe only work against the full domain. Session Storage, as the name suggests, only exists for the session... and Local Storage is subject to a lot of privacy settings which don't allow anything to be written....

 

 

So when creating a custom solution, those limitations needs to be considered.

 

The other, much more technical solution might be to work with the developers... if the users are logged in to be able to do anything, you could even consider storing information into the user's account rather than relying on anything in the browser... but obviously this has the drawback of needing to know who the user is and having them logged in...

 

Or, you might be able to get your developers to create a server-side cookie tied to certain events? 

 

Those last two options require developer involvement, but it depends on how important this is, and if this will yield the best results for you.

Avatar

Level 2

Thank you this solution works. 

 

I am also curious to know the best method to setup the same for mobile app.

Avatar

Community Advisor

Glad that it works.

For mobile, I am unfortunately not so deep into the matter to give a qualified answer

Cheers from Switzerland!


Avatar

Community Advisor and Adobe Champion

Mobile App has always been a custom solution, though ironically it should be easier (assuming the flow is 100% located in the app)

 

Since the app is its own little application, you would need to get the developers to keep track of the time. In order to match with your website, you will need to ensure that you are using the same unit of measure (i.e. seconds maybe?).

 

In the apps, there is a lot of information that can be stored (and not by the same limited ways we have in web)... you would instruct your developers to save a timestamp on Action A (which you would track your event), then on Action B (where you would track your second event), and then ask the developers to calculate the time between the events and pass it to Adobe in the same key that web uses.

 

Now, if there is a chance that the flow could start on web and end on mobile app, or start on the app and end on the website... I don't think that is possible....