简体   繁体   中英

How to modify google analytics code using google tag manager

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.

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配置

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM