2

My default index html like this :

<html>

<head>

    <meta name="google-site-verification" content="****************" />

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

  gtag('config', 'AW-7**4*7***');
</script>
</head>

<body>

<script src="/Content/assets/script/combined.min.js" type="text/javascript"></script>

<script async src="https://www.googletagmanager.com/gtag/js?id=UA-*4**0**6-*"></script>

<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-*4**0**6-*');
</script>
</body>

</html>

So I using google tag manager

But I want to move the script inline js to the external js file (combined.min.js). So there is no inline js in the default html. This is to optimize my website

Other than that I also want to change the call js. So I want to call the js file from the folder. Not online. So like this :

For example :

I want to change to be like this :

So I copied the contents of the file to my folder

So the result like this :

<html>

<head>

  <meta name="google-site-verification" content="****************" />

  <script async src="/Content/assets/script/gtag-aw.min.js"></script>
</head>

<body>

<script src="/Content/assets/script/combined.min.js" type="text/javascript"></script>

<script async src="/Content/assets/script/gtag-ua.min.js"></script>

</body>

</html>

Can I do it like that? Or there is a better way?

Update :

I made combine.min.js like this :

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'AW-7**4*7***');

  ...
  /* some javascript files, for example jquery, jquery ui, custom js and others */
  ...

  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-*4**0**6-*');

What do you think?

2 Answers 2

2

The code examples given are not Google Tag Manager Script It is Global Site Tag gtag.js tracking code snippets and both the AW and GA should be combined into a single snippet

The global site tag (gtag.js) is JavaScript tagging framework and API that allows you to send event data to Google Ads, Campaign Manager, Display & Video 360, Search Ads 360, and Google Analytics. Instead of having to manage multiple tags for different products, you can use gtag.js and more easily benefit from the latest measurement features and integrations as they become available.

Should you Use GTM or gtag.js?
https://developers.google.com/gtagjs#google_tag_manager_or_gtagjs

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

  gtag('config', 'UA-*4**0**6-*');
  gtag('config', 'AW-7**4*7***');
</script>

Configuring a single snippet for multiple products:
https://developers.google.com/gtagjs/devguide/add

Keep in mind, if the script needs to wait for the page to render before firing off tracking and conversion hits to various products, you may end up with inaccurate data

1

Yes. You can load it from an external file like any JS.

However, do be aware about loading order. If you need the GTM to pick up on events that occur before the page has finished loading, calling from another file might mean it's not loaded yet (this is largely dependant on what you're tracking and how long your page load times are).

5
  • seems like it can't. because the inline script is one in the head and the other in the body. it's not possible to merge them to the combine.min.js file Sep 24, 2019 at 13:43
  • Load it all in the body. But be aware of the issues with it not loading in time to catch some events. Sep 24, 2019 at 13:45
  • can the two inline script files be added to the combined.min.js file? will it not affect Google Analytics later? Sep 24, 2019 at 13:49
  • Loading external scripts in a JS file is something for StackOverflow. Thankfully, it's one they've already answered: stackoverflow.com/questions/950087/… Sep 24, 2019 at 13:51
  • Try to see my question. I had update it Sep 24, 2019 at 14:02

Your Answer

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

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