Embedding Google Analytics GTAG and GTM Scripts

This forum is for all Flare issues related to the HTML5, WebHelp, WebHelp Plus, and Adobe Air Targets
Post Reply
lorynj
Propeller Head
Posts: 34
Joined: Mon Apr 23, 2018 6:17 pm
Location: Melbourne, Australia

Embedding Google Analytics GTAG and GTM Scripts

Post by lorynj »

I recently had some difficulty adding GTAG and GTM Scripts to a site. Thought I'd share the solutions here.

When I embedded the GTAG script into the Masterpage header…

Code: Select all

		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-XX"></script>
		<script>
			window.dataLayer = window.dataLayer || [];
			function gtag(){dataLayer.push(arguments);}
			gtag('js', new Date());

			gtag('config', 'UA-XXXXXXXXX-XX');
		</script>
…the code analyzer detected a syntax error and the compiler failed with an error.

But adding ="async" to the async parameter fixed it.

Code: Select all

		<!-- Global site tag (gtag.js) - Google Analytics -->
		<script async="async" src="https://www.googletagmanager.com/gtag/js?id=UA-XXXXXXXXX-XX"></script>
		<script>
			window.dataLayer = window.dataLayer || [];
			function gtag(){dataLayer.push(arguments);}
			gtag('js', new Date());

			gtag('config', 'UA-XXXXXXXXX-XX');
		</script>
Then I changed to using GTM. The code analyzer detected an issue within the GTM script.

Code: Select all

		<!-- 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','GTM-XXXXXXX');</script>
		<!-- End Google Tag Manager -->
But wrapping the script in the CDATA declaration prevented Flare from trying to analyze it.

Code: Select all

		<!-- Google Tag Manager -->
		<script>/*<![CDATA[*/(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','GTM-XXXXXXX');/*]]>*/</script>
		<!-- End Google Tag Manager -->
So, it's all working for me now. Hope this helps someone.
Loryn Jenkins
Technical Writer and Consultant
Franchise Cloud Solutions
AlexFox
Sr. Propeller Head
Posts: 149
Joined: Thu Oct 19, 2017 1:56 am

Re: Embedding Google Analytics GTAG and GTM Scripts

Post by AlexFox »

The error in your script may well be caused by the & symbol. You usually have to change these to & due to the strictness of XML.
IPRO_JB
Propeller Head
Posts: 13
Joined: Mon Jan 29, 2018 1:31 pm

Re: Embedding Google Analytics GTAG and GTM Scripts

Post by IPRO_JB »

Hey Lorynj,
Just wanted to let you know that your solution worked for us. We were struggling this this and then I found your post. We changed our code and, viola, no more compile errors. Thank you for providing the details that you did. They helped me and my tech guy sort out our issue, build, and publish successfully. :D
All the best,
Julie
sboltz
Propeller Head
Posts: 25
Joined: Wed Sep 10, 2014 9:59 am

Re: Embedding Google Analytics GTAG and GTM Scripts

Post by sboltz »

Thank you Loryn! We're looking at adding GA to our help and this did the trick. Now for the fun stuff!
Suzette
aevansb
Propeller Head
Posts: 10
Joined: Tue Mar 11, 2014 12:50 pm

Re: Embedding Google Analytics GTAG and GTM Scripts

Post by aevansb »

Thanks for posting! This solution worked for us!
Adam Evans in Chicago
Sr Technical Documentation Manager at Strata Decision Technology
RickD
Propeller Head
Posts: 38
Joined: Wed Apr 29, 2015 11:19 pm

Re: Embedding Google Analytics GTAG and GTM Scripts

Post by RickD »

Many thanks, all.

This worked for me as well. Used the <script>/*<![CDATA[*/ to get Flare to ignore the syntax error and compile it.

If more sites are using Google Tag Manager, Madcap may need to look at supporting the syntax.
Post Reply