Google Analytics _trackEvent doesn't work with

2019-03-03 23:21发布

问题:

I have a php file generating XHTML content and I use

<?php header('Content-type: application/xhtml+xml');?>

at the top of the page to do so. However, my google analytics event tracking absolutely refuses to work when that tag is present. If I remove that tag, event tracking begins to fire again. I am not altering the tracking code at all, just removing that one php tag defining the page as XHTML.

<a href="some-file.zip" onClick="_gaq.push(['_trackEvent', 'Item', 'Download', 'file-name']);">Download File</a>

I need the page to be defined that way so Safari and Firefox 3.6 will correctly render the SVG images I have on there, so removing it isn't an option unless there's some other way to make Safari and Firefox 3.6 render SVG images.

Edit: I'm debugging by tracking everything with Firebug.

回答1:

Have you tried removing the javascript from the inline anchor element and creating a listener which is wrapped in a CDATA tag?

Example:

<a id="my_anchor" href="someZip.zip">Click me</a>

<script type="text/javascript">
/* <![CDATA[ */

// Ensure scope for _gaq ....

// Jquery syntax..
$('#my_anchor').click(function()
{
  _gaq.push(['_trackEvent', 'Item', 'Download', 'file-name']);
});

/* ]]> */
</script>