开发者

Google Analytics and _trackEvent: Events not logging to Analytics

I pieced together a couple of ideas I found online around tracking scroll distance on long article pages in Analytics. I'm interested in logging an event when the user reaches the halfway mark in the article, and then again at the end point. My code isn't perfect yet as it's not accounting for time on page (what if the user scrolls to the bottom as soon as the page loads?), but that's an easy issue to fix once I get this issue taken care of.

With the code below the two console.log() calls are tracing exactly when they should be, so my assumption was that the _trackEvent calls would also work, but they're not. No error is thrown. They just never show up in Analytics.

Can anyone see any glaring errors or holes? Here is the site where I'm using this code: http://www.adlucent.com/blog/

$(document).ready(function() {

    if ($(document.body).hasClass('single')) {
        var doc_height = $(document).height(),
            article_pos = $('#main > article').offset(),
            article_height = $('#main > article').height(),
            article_half = (article_height/2) + article_pos.top,
            article_bottom = article_height + article_pos.top,
            scrolled_half = Math.round(100 * article_half / doc_height),
            scrolled_whole = Math.round(100 * article_bottom / doc_height);

        $(window).scroll(function(){
            var scrollPercent = GetScrollPercent();
            if (!isScrolledHalf) {
                if (scrollPercent > scrolled_half) {
                    isScrolledHalf = true;
                    co开发者_如何学JAVAnsole.log("Reached halfway mark!");
                    _gaq.push(['_trackEvent', 'Content Engagement', 'Scroll', 'Half']);
                }
            }
            if (!isScrolledWhole) {
                if (scrollPercent > scrolled_whole) {
                    isScrolledWhole = true;
                    console.log("Reached the end!");
                    _gaq.push(['_trackEvent', 'Content Engagement', 'Scroll', 'End']);
                }
            }
        });
    }

});
function GetScrollPercent(target){
    var bottom = $(window).height() + $(window).scrollTop(),
        height = $(document).height();
    return Math.round(100 * bottom / height);
}


As it turns out it may actually be a bug in the new GA. I'll report back as a comment here when I hear back.

LATER...

I never received an answer on this issue, but my code eventually started tracking. I later switched to using this to track scroll depth and it works absolutely flawlessly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜