1

I want to add Google Analytics and Google Ads tags to my website:

This is for Google Analytics:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script type="text/javascript">
   window.dataLayer = window.dataLayer || [];
   function gtag() { dataLayer.push(arguments); }
   gtag('js', new Date());
   gtag('config', 'UA-xxxxxxxxx-x', { cookie_domain: 'my-domain.com', cookie_flags: 'SameSite=None; Secure' });
</script>

This is for Google Ads:

<script async src="https://www.googletagmanager.com/gtag/js?id=AW-yyyyyyyyy"></script>
<script>
   window.dataLayer = window.dataLayer || [];
   function gtag() { dataLayer.push(arguments); }
   gtag('js', new Date());
   gtag('config', 'AW-yyyyyyyyy');
</script>

How should I combine them in one script to be added to my site:

Should I do this:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script async src="https://www.googletagmanager.com/gtag/js?id=AW-yyyyyyyyy"></script>
<script type="text/javascript">
   window.dataLayer = window.dataLayer || [];
   function gtag() { dataLayer.push(arguments); }
   gtag('js', new Date());
   gtag('config', 'UA-xxxxxxxxx-x', { cookie_domain: 'my-domain.com', cookie_flags: 'SameSite=None; Secure' });
   gtag('config', 'AW-yyyyyyyyy');
</script>

or should I do this:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script type="text/javascript">
   window.dataLayer = window.dataLayer || [];
   function gtag() { dataLayer.push(arguments); }
   gtag('js', new Date());
   gtag('config', 'UA-xxxxxxxxx-x', { cookie_domain: 'my-domain.com', cookie_flags: 'SameSite=None; Secure' });
   gtag('config', 'AW-yyyyyyyyy');
</script>

Note: I have seen this question but it does not answer what I am after.

2 Answers 2

0

You only need a single call to googletagmanager.com/gtag

3
  • 1
    Can you elaborate a little bit more on this?
    – John Conde
    Feb 16, 2023 at 13:08
  • Thanks but what id should I use in that call? Feb 16, 2023 at 19:42
  • 1
    The ID is the primary ID for the gtag. I would use the AW one, as UA is going away soon. If you follow what happens on the network, it will actually make extra gtag requests for each other ID specified in a config command. So you only need to do one yourself, to get the ball rolling. Feb 17, 2023 at 2:43
0

According to this answer, here is how we can merge the two tags:

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxxxx-x"></script>
<script type="text/javascript">
   window.dataLayer = window.dataLayer || [];
   function gtag() { dataLayer.push(arguments); }
   gtag('js', new Date());
   gtag('config', 'UA-xxxxxxxxx-x', { cookie_domain: 'my-domain.com', cookie_flags: 'SameSite=None; Secure' });
   gtag('config', 'AW-yyyyyyyyy');
</script>

And here is the documentation from Google.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.