Reputation: 5
i have a problem with the ga() function when i use google tag manager.
i inserted google tag manager to my script. here is the demo script:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-00000');
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
</head>
<body>
<!-- Google Tag Manager (noscript) -->
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id=GTM-00000"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<!-- End Google Tag Manager (noscript) -->
<a href='#' onClick="ga('send', 'event', 'TESTBUTTON', 'klick', 'test1' ,1);">click</a>
</body>
</html>
if i click on the link i would like to track the event. but it doesn't. Analytics shows me that somebody is on the page but doesn't track the click.
then i tried to insert google analytics and not google tag manager like:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<!-- Google Tag Manager -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Unbenanntes Dokument</title>
<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','https://www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-0000000-0', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<a href='#' onClick="ga('send', 'event', 'TESTBUTTON', 'klick', 'test1' ,1);">click</a>
</body>
</html>
With analytics it works. i added analytics to my tag manager so i don't understand what the problem is.
Upvotes: 0
Views: 445
Reputation: 8907
If you use GTM you should consider removing all onpage GA code so that you avoid these issues, but if it's absolutely necessary then read along. When you use GTM and create tags, GTM automatically uses its own internal tracker name (something like gtm22345677664
) for every single tag. Your other on-page code, though referencing thega
object, is not using one of the tracker names given by GTM, so in a way there is a miscommunication, and is in a way "unnamed" (though technically it still does have a name, but for all intents and purposes let's say it doesn't). Your "unnamed" tracker is sending data somewhere else. To rectify this, you need to tell your GTM tags to not use its own naming convention and to leave the tags unnamed or use their original names. To do that just check the box that says Set tracker name
but don't put anything into the box. Once you've done this you should see your onpage tracker also start to collect data.
Upvotes: 1