Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts

    Track sections of content on single page as virtual page view in Google Analytics

    0 down vote favorite

    I'm trying to track when a "section" of a single page website is in the viewport and send that to Google Analytics as a virtual page view.

    A few notes:
    • I'm not a javascript coder but have a very rough idea on how things work in terms of inserting scripts into pages and the like
    • Already calling jquery 1.8
    • Google analytics is working on the site and tracking menu clicks and time on page etc all very well.
    • The sections are defined by div's with unique IDs e.g. #section-about
    • I'm now trying to capture tracking info for people that do not use the menu but just scroll down the page and view each section
    • I'd only want to capture a "page view" if the section was in view for longer than say 2 seconds.

    I found this which looks to get me part of the way there but I'm at a loss at so how to customise it:

        var tracking_active = '';
        var tracking = {
            'section-approach': '/testing/section-approach',
            'section-about': '/testing/section-about'
        function scrollTrackPageview(){
            var section = '';
            $.each(tracking, function(key, value) {
                section = value;
                if($(window).scrollTop() >= $('.'+key).offset())
                    return false;
                return true;
            if(!section || section == tracking_active)
                return false;
            _gaq.push(['_trackPageview', section]);
            //console.log('Bereich: '+section);
            tracking_active = section;
        $(window).on("scroll", scrollTrackPageview);
    Thanks in advance for the time and effort.

  2. #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Thanked 256 Times in 252 Posts
    You said you are using ids but you are doing $('.'+key) and not $('#'+key).

    You also need to use debounce function in order to prevent firing scrollTrackPageview() multiple times for every scroll.

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins

  3. #3
    New to the CF scene
    Join Date
    Feb 2014
    Thanked 0 Times in 0 Posts
    Thanks very much. I'll give this a go and see how I get on.


Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts