1
votes

I have the google analytics tracking code running on my site which I have added through google tag manager. The code from google analytics in general looks like this:

<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-XXXXXXXX-1', 'auto');
ga('send', 'pageview');

however since I have added it through tag manager I only have the tag manager code in my page code which is:

<!-- Google Tag Manager -->
<noscript><iframe src="//www.googletagmanager.com/ns.html?id=XXX-XXXXXX"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
<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=
'//www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','XXX-XXXXXX');</script>
<!-- End Google Tag Manager -->

Now I would need to add some lines of code in the google analytics code between:

ga('create', 'UA-XXXXXXXX-1', 'auto');

add *code* here

ga('send', 'pageview');

How can I do that? I did not find any possibility yet to modify the original analytics code within tag manager so would appreciate any assistance.

1
As Adam pointed out you usually do not need to change the code; you can do almost everything (setting fields, setting custom dimensions and metrics, configure cross domain tracking etc) via the "More settings" tab in the GA tag template. Pretty much the only thing that does not work is using Google Analytics plugins.Eike Pierstorff
I had the same issue deploying google Optimize in tag manager following these instructions that made me confused.Soorena

1 Answers

3
votes

That depends on what you want to do with that code.

Option 1

If it's something 'standard', Tag Manager should give you a fairly straight-forward form when you add a Universal Analytics tag - you can use this to set certain options within the tag:

GTM GA config

Option 2

If this doesn't cover it, you can change the type of your GA tag to a custom js tag, put the original snippet in and make your changes directly.