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

Embedding Google Analytics GTAG and GTM Scripts

Postby lorynj on Wed Jan 16, 2019 5:27 pm

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

Image
lorynj
Propeller Head
 
Posts: 24
Joined: Mon Apr 23, 2018 6:17 pm
Location: Melbourne, Australia

Re: Embedding Google Analytics GTAG and GTM Scripts

Postby AlexFox on Mon Jan 21, 2019 10:05 am

The error in your script may well be caused by the & symbol. You usually have to change these to &amp; due to the strictness of XML.
AlexFox
Sr. Propeller Head
 
Posts: 127
Joined: Thu Oct 19, 2017 1:56 am

Re: Embedding Google Analytics GTAG and GTM Scripts

Postby IPRO_JB on Thu Feb 28, 2019 7:55 am

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
IPRO_JB
Jr. Propeller Head
 
Posts: 8
Joined: Mon Jan 29, 2018 1:31 pm

Re: Embedding Google Analytics GTAG and GTM Scripts

Postby sboltz on Fri Mar 15, 2019 1:23 pm

Thank you Loryn! We're looking at adding GA to our help and this did the trick. Now for the fun stuff!
Suzette
sboltz
Propeller Head
 
Posts: 18
Joined: Wed Sep 10, 2014 9:59 am

Re: Embedding Google Analytics GTAG and GTM Scripts

Postby aevansb on Fri Apr 19, 2019 12:35 pm

Thanks for posting! This solution worked for us!
Adam Evans in Chicago
Sr Technical Documentation Manager at Strata Decision Technology
aevansb
Jr. Propeller Head
 
Posts: 9
Joined: Tue Mar 11, 2014 12:50 pm

Re: Embedding Google Analytics GTAG and GTM Scripts

Postby RickD on Mon Apr 22, 2019 6:27 pm

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.
RickD
Propeller Head
 
Posts: 37
Joined: Wed Apr 29, 2015 11:19 pm


Return to Web-based Outputs

Who is online

Users browsing this forum: No registered users and 4 guests