Expand my Community achievements bar.

Join us for our next community Coffee Break on February 28th! Four of our Analytics Champions will be joining us to discuss Summit tips, best practices, and any of your Analytics questions!

Percent page load installation within WebSDK

Avatar

Level 1

We are trying to get the the percentage paged viewed.

I know previously there was a plug in (https://experienceleague.adobe.com/docs/analytics/implementation/vars/plugins/getpercentpageviewed.h...) however, it is not supported for the WebSDK. 

 

Is there a way to get this information before? 

1 Reply

Avatar

Community Advisor

It may not be the easiest, but you should be able to come up with a custom solution using JS and session storage values...  (if you aren't comfortable with coding, you may want to get a developer from your team to help you).

 

 

But in theory, when the page loads, you can get the height of the page with:

window.document.body.clientHeight

*** Important Note: If your developers have done something silly like set the CSS of the body to 100%, this will only get the height of the visible viewport, and then your scroll position will be greater than the "height" of the page resulting in wonky numbers... you may need to find alternate code to get the height of the body.

 

 

To get the user's scroll position you can use:

Math.round(window.scrollY)

 

Now, the user may resize the page at any given time while on your site, so you are going to want to evaluate both values... I would recommend using the scrollend event (rather than scroll.. so that you only evaluate the % completed when the user finishes the scroll rather than every few pixels of the scroll while in progress).

 

document.addEventListener("scrollend", function(){
    var height = window.document.body.clientHeight;
    var position = Math.round(window.scrollY);
    var percentScrolled = position/height;
    percentScrolled = percentScrolled.toFixed(2);
    var lastMaxPercent = sessionStorage.getItem("percentScrolled");
    if (lastMaxPercent < percentScrolled){
        sessionStorage.setItem("percentScrolled", percentScrolled);
    }
});

 

 Basically, each time the user scrolls, this will evaluate the height of the page and the user's scroll position and calculate the % scrolled...  Then it will check session storage for a lower value (no session storage will be considered "lower") and the value of the session storage will be overwritten with the higher value.

 

You will also need to store the current page identifier to a session storage value as well.

 

When a page is loaded, you must read the two session variables first, and extract the values for use in your tracking, then immediately delete BOTH of them or force an overwrite for the current page so that the new scrolling can start to work for this page.

 

Essentially the rules must go something like this:

  1. Get Value of Session Variables to Include on the Page View (for previous page)
  2. Page View Beacon Fired
  3. Clear / Reset Session Variables so that the new page height and scroll position will start to be evaluated
  4. Start the new Scroll Tracking code for the new page

 

This may require some tweaks, as this is just off the top of my head, but essentially this should work for you.