Back to Code List
javascript

Percent Page Viewed Plugin

This is the SiteCatalyst Percent Page Viewed plugin that has been adapted to work with any analytics you desire. Instructions can be found in the code and the tooltips file. This should be run on a page load typically and it will output the percent page viewed for the previous page you were on.

   tooltips.txt

tooltips:

Page Viewed:
    Returns the previous page's percent of the page viewed and then resets. See additional extension notes.
Additional Extension Info:
    An extension to keep track of the percent of a page viewed. It will update when needed on page resize, scrolling, etc.
    If landing on the site the first time, this will not return a value. Any page thereafter will report the percentage (e.g. 57) of the previous page viewed.
    If using this with a link call, it will report the percentage of the page viewed that the user clicked the link on (their current page).
    RECOMMENDED: This extension can be extra useful in conjunction with the "get previous value" extension. If that captures the page name or url, this can then be correlated for effective reporting.    

   code_to_paste.js

// change these variables
    var output_name = "ppv";
    
    //the variable name listed above will output
    //into this variable. Assign after the function
    //runs as you need.
    var b = {};
// end changes

//this plugin should only run on a page view typically.
//this variable should be altered for non-page views
var a = "view";

if(a=="view"){
    (function (u,c,p,a,r,q) {
        if (!window._ppv) window._ppv = {};
        r = function (w, x, y, z) {
            // read cookie
            x = w + "=";
            y = document.cookie.split(';');
            for (var i = 0; i < y.length; i++) {
                z = y[i];
                while (z.charAt(0) == ' ')
                    z = z.substring(1, z.length);
                if (z.indexOf(x) == 0)
                    return z.substring(x.length, z.length);
            }
            return "";
        };
        q = function (x, y, z) {
            // set cookie
            if (typeof z == "number") z = new Date(z);
            document.cookie = x + "=" + y + ";path=/;domain=" + location.hostname + ";expires=" + (z?z.toGMTString():"");
            //b["cp."+x] = y; // add cookie to udo
        };
        window._ppv.hppv = function (w,x,y,z,r,q,c) {
            if (!window._ppv.getPPVid)
                return;
            c = "utag_ppv";
            r = function (w, x, y, z) {
                // read cookie
                x = w + "=";
                y = document.cookie.split(';');
                for (var i = 0; i < y.length; i++) {
                    z = y[i];
                    while (z.charAt(0) == ' ')
                        z = z.substring(1, z.length);
                    if (z.indexOf(x) == 0)
                        return z.substring(x.length, z.length);
                }
                return "";
            };
            q = function (x, y, z) {
                // set cookie
                if (typeof z == "number")
                    z = new Date(z);
                document.cookie = x + "=" + y + ";path=/;domain=" + utag.cfg.domain + ";expires=" + (z?z.toGMTString():"");
                //b["cp."+x] = y; // add cookie to udo
            };
            w = Math.max(
                    Math.max(document.body.scrollHeight, document.documentElement.scrollHeight), 
                    Math.max(document.body.offsetHeight, document.documentElement.offsetHeight), 
                    Math.max(document.body.clientHeight, document.documentElement.clientHeight)
                ),
            x = (window.pageYOffset || (document.documentElement.scrollTop || document.body.scrollTop))
                +(window.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight));
            y = Math.min(Math.round(x / w * 100), 100);
            z = (r(c).indexOf(',') > -1) ? r(c).split(',', 4) : [];
            q(c, ((y > 0) ? (((z.length > 0) ? (z[0]) : escape(window._ppv.getPPVid)) + ',' + ((y > ((z.length > 1) ? parseInt(z[1]) : (0))) ? y : ((z.length > 1) ? parseInt(z[1]) : (0))) + ',' + ((z.length > 2) ? parseInt(z[2]) : (y)) + ',' + ((x > ((z.length > 3) ? parseInt(z[3]) : (0))) ? x : ((z.length > 3) ? parseInt(z[3]) : (0)))) : ''));
        };
        c = "_ppv";
        p = p ? p : '-';
        a = (r(c).indexOf(',') > -1) ? r(c).split(',', 4) : [];
        if (a.length < 4) {
            for (i=3; i>0; i--) {
                a[i] = (i < a.length) ? (a[i - 1]) : ('');
            }
            a[0] = '';
        }
        a[0] = unescape(a[0]);
        q(c, escape(p));
        if (!window._ppv.getPPVid) {
            window._ppv.getPPVid = (p) ? (p) : document.location.href;
            q(c, escape(window._ppv.getPPVid));
            if (window.addEventListener) {
                window.addEventListener('load', window._ppv.hppv, false);
                window.addEventListener('scroll', window._ppv.hppv, false);
                window.addEventListener('resize', window._ppv.hppv, false);
            } else if (window.attachEvent) {
                window.attachEvent('onload', window._ppv.hppv);
                window.attachEvent('onscroll', window._ppv.hppv);
                window.attachEvent('onresize', window._ppv.hppv);
            }
        }
        
        b[u] = (p != '-') ? (a) : (a[1]);
    })(output_name);
}