2

I've been experimenting with auto generating events for google tag manager.

We have a script that we have used with traditional async analytics that generates onclick events for downloads, emails and external clicks.

I modified it to generate a GTM event instead of a standard analytics event as follows:

if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function($) {
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        var baseHref = '';
        if (jQuery('base').attr('href') != undefined)
            baseHref = jQuery('base').attr('href');
        jQuery('a').each(function() {
            var href = jQuery(this).attr('href');
            if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                jQuery(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    dataLayer.push  ({'eventCategory' : 'External', 'eventAction' : 'click', 'eventLabel': extLink, 'event' : 'autoevent'});
                    if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                        setTimeout(function() { location.href = href; }, 200);
                        return false;
                }
            });
        }
        else if (href && href.match(/^mailto\:/i)) {
            jQuery(this).click(function() {
                var mailLink = href.replace(/^mailto\:/i, '');
                dataLayer.push({'eventCategory' : 'Email', 'eventAction' : 'click', 'eventLabel': mailLink, 'event' : 'autoevent'});
            });
        }
        else if (href && href.match(filetypes)) {
            jQuery(this).click(function() {
                var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                var filePath = href;
                 dataLayer.push({'eventCategory' : 'Download', 'eventAction' : 'click', 'eventLabel': filePath', 'event' : 'autoevent'});
                if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
                    setTimeout(function() { location.href = baseHref + href; }, 200);
                    return false;
                }
            });
        }
    });
});
}

Inside GTM I would listen for an event called autoevent, which would generate an event tag with values for Category, Action and Label.

I have been experimenting with a wordpress site so far.

My first attempt had the above script declared in the head. The GTM code was in the body, as required.

Using the GA debugger in chrome, I am getting a JS error Uncaught SyntaxError: Unexpected identifier on the line that issues the dataLayer push.

My Javascript sux, so I took a guess and declared the dataLayer above the GTM code, and moved the auto generating script below the GTM code, so that the dataLayer would be fully declared. Same error occurs.

Any suggestions?

4

1 回答 1

0

您在 filePath 上有一个额外的单引号:

 dataLayer.push({'eventCategory' : 'Download', 'eventAction' : 'click', 'eventLabel': filePath', 'event' : 'autoevent'});
于 2013-08-12T20:24:20.683 回答