javascript - Tracking Scrolling Virtual Page Views in GA -
update: looking making sure script not cache events, search has not gone @ far.
i using script below track virtual page views in ga, site has long homepage tracking on. problem having once user scrolls down lower section, not register new page view if scroll (making maximum of 1 view per page). appreciated. using jquery , script below send/track.
this script in addition universal analytics code being implemented before tag. first code below normal analytics code, needed run debugger because account define. second script 1 having issue with.
normal ua script (located in head tags):
<script> (function(i,s,o,g,r,a,m){i['googleanalyticsobject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new date();a=s.createelement(o), m=s.getelementsbytagname(o)[0];a.async=1;a.src=g;m.parentnode.insertbefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'ua-123456-1', 'auto'); ga('require', 'displayfeatures'); ga('send', 'pageview'); </script>
script scrolling virtual page views:
<script language="javascript"> // predefined variable frequency = 10; //tracking , sending _frequency = frequency; _repetition = 100 / frequency; var _scrollmatrix = new array(); (ix = 0; ix < _repetition; ix++) { _scrollmatrix[ix] = [_frequency, 'false']; _frequency = frequency + _frequency; } $(document).scroll(function (e) { (iz = 0; iz < _scrollmatrix.length; iz++) { if (($(window).scrolltop() + $(window).height() >= $(document).height() * _scrollmatrix[iz][0] / 100) && (_scrollmatrix[iz][1]== 'false')) { _scrollmatrix[iz][1] = 'true'; ga('send', 'pageview', _scrollmatrix[iz][0]+'%' ); } } }); </script>
you script creates array of arrays threshold values ranging 10 100 index, determined "repentance" variable (this not mean think means; mean "repetitions", unless sorry writing script). 1 of values each element if array of array boolean "false".
this boolean value evaluated in loop inside document.scroll function. if value "false" pageview executed, , variable set "true". happens in bit:
if (($(window).scrolltop() + $(window).height() >= $(document).height() * _scrollmatrix[iz][0] / 100) && (_scrollmatrix[iz][1]== 'false')) { _scrollmatrix[iz][1] = 'true';
that means next time around boolean _scrollmatrix[iz][1]
"true" , if branch no longer executed.
if want fire google analytics code regardless can remove boolean value , part it's evaluated. script this:
<script language="javascript"> // predefined variable frequency = 10; //tracking , sending _frequency = frequency; _repentance = 100 / frequency; var _scrollmatrix = new array(); (ix = 0; ix < _repentance; ix++) { _scrollmatrix[ix] = [_frequency]; _frequency = frequency + _frequency; } $(document).scroll(function (e) { (iz = 0; iz < _scrollmatrix.length; iz++) { if (($(window).scrolltop() + $(window).height() >= $(document).height() * _scrollmatrix[iz][0] / 100) ) { ga('send', 'pageview', _scrollmatrix[iz][0]+'%' ); } } }); </script>
while script tracking have point out resulting ga code wrong. tracking calls this:
ga("send", "pageview", "10%") ga("send", "pageview", "20%") ...
you cannot put in naked percentage argument function , hope gets recorded somewhere (edit oops, bad. of course third parameter works virtual pagename, bit alright if want percentage pagename).
since script executes loop on every scroll event sends dozens of ga calls, , computed percentages not relate actual scroll position of user (it sends values 10-100% each scroll event). plus looping on every scroll event makes horribly expensive in terms of browser ressources. , of course have quota of hits per second , hits per sessions, , script hit quotas pretty quickly.
i frankly recommend abandon script , use 1 of existing tested solutions. jquery scrolldepth plugin quite nice , should able adapt needs (more rewriting own).
Comments
Post a Comment