Is it possible to host the scripts generated by Google Analytics and especially Tag Manager on the executing server, rather than fetching them client side, through Google's script block?
The goal is to avoid any dependencies on external scripts.
I understand it is not Google's recommendation to host neither analytics, nor tag manager locally, but is it possible to do so?
As I understand, tag manager works, by embedding a local script like so:
<!-- 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','YOUR-GTM-CODE');</script>
<!-- End Google Tag Manager -->
When this executes client side, it fetches a newly generated script (https://www.googletagmanager.com/gtm.js?id=YOUR-CODE), containing any new tags or triggers your editors have added to the container since last publish.
Apart from losing new tag manager features, and newly generated tags, will this work?
gtm.js
. If you don't trust Google to serve safe JavaScript, you might want to consider using a different analytics provider. – mootrichard