I've inserted Google Analytics tracking code into the header/footer of master pages in RH 2017 and older, without issue. Trying to do the same with a brand new project created in RH 2019 and am having trouble.
If I insert the following code (I've replaced my tracking id with Xs for this post) in the header of a master page, as directed by Google:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXXX"></script>
window.dataLayer = window.dataLayer || ;
gtag('js', new Date());
RoboHelp identifies the following error:
The attribute [ async ] must have a value.
Invalid character s
Additionally, RoboHelp won't let me switch from the master page's code-view to its edit-view. Instead, it pops up a "Parse Error" dialog at the bottom right corner of RH stating:
Line: 6, Column: 15, Message: Invalid character s
Finally, while RoboHelp will allow me to save the change, if I attempt to create a new topic using this master page, the Google Analytics script/code is omitted from the header of the NEW topic. Note: I've also tried this with the footer; the behavior/result is the same.
RoboHelp 2019 you said
Classic or the new baby?
On the Madcap forums, async always causes an error in Flare.
Change it to async="" and it shouldn't throw an error any more.
EDIT: That's recommended by the scripting guru on the madcap forums. But if it doesn't work try asyn="async" as per w3 schools https://www.w3schools.com/tags/att_script_async.asp
Removing "src" and going with just "async" resolved the RoboHelp error. Thank you!
Fixed the async/src line. Thank you!
Last issue. Google advises inserting the Google Analytics (GA) script between <head> </head>
I was hoping to deploy the GA script via RoboHelp master pages, so when I create a new topic, the script is auto-added (this is a new project). And similarly, if I decide to remove GA from the project, I could simply remove it from the master page header and it'd be removed from all the corresponding topics. However, in RoboHelp, the Master Page topic headers and footers are actually implemented via <div data-region="header"> </div> and <div data-region="footer"> </div>. I can insert the GA script in one of those (and new topics get it - and likewise remove it from the master page and it's removed from the same topics), but does anyone know if GA will work, since it's not in the <head> section, as they advise?
Or, am I going to need to actually insert the GA script in the <head> section of topics - in which case my only options for automating the insertion/removal will be via duplicating an existing topic (that has it) and/or a carefully crafted global search and replace procedure (e.g., using FAR HTML's search and replace tool or Notepad++ (long-time RH user, never trusted their search and replace tool, but maybe it's improved over the last 15 years and is now on par with those others))?
I believe the old ga() code used to work when added to the master page header or footer, but I'm not currently using gtag, master page header footers or RH2019 (new or otherwise) so can't comment definitively. You might need to do a test release to confirm.
The correct place to put the script tag would be inside <head> tag of Master Page, not inside <div data-region="header"> or footer. The Master Page header and footer regions become actually part of Topic's Visible Content.
However as of now, RH 2019 doesn't copy <script> tags from Master Page <head> tag into Topics. We will fix this issue in upcoming release soon.
Please track the issue here: https://tracker.adobe.com/#/view/RH-6359.
Amebr and Vikas,
Thank you for the info! Looking forward to this getting patched - I'll work around it in the meantime!
@vikassingla As I remember, in RH11 entering the script in the master page "header" div adds the script into the project (into the topic header div, not into the project head tag) and the google analytics code works. I always switched to code view to add it, but I think using Insert > Script might also have added it as long as the cursor was in the right spot? I'd have to check though.
I did a quick test in RH2019 New adding a fake script into the header div using code view, and that is added into the output in a similar spot.
Adding the script in to the master page <head> tag never worked.
Not saying this is the right way, just describing how RH previously worked and still appears to.
If you have more specific information about the latest gtag script, I'd appreciate it.
@RoboFan, it might be worth checking that adding the code into the header div copies it to the output like I describe. And test that it sends data from a website to google if you can. 🙂
On a side note, I feel like last time I looked, the advice for the analytics code was really confusing and there is information floating around from 3 different script versions ago. I thought last time I looked that I even saw advice to put the script code just above the closing body tag. As I said, confusing. I do think it's worth testing if you can.
Thank you Amebr for trying out gtag script in RH 2019. You have explained correctly that when added in header region of Master Page, it would work fine in Topics.
Google recommends putting the script tag in <head> tag of HTML files, so to achieve the same, RH 2019 needs to copy <script> tags from <head> section of Master Pages into Output Topics' <head>. That is the part we will be fixing.
However, @RoboFan you can go ahead with adding <script> tag in Master Page header region div. It will be included in final output topic, except the fact that it will be inside <body> of topic, not inside <head>.
As Amebr has tried out, it should still work fine.
In older RH version projects, I have a GA script set up in the footer (<?rh-region_start type="footer">) of master pages - and it worked: that is, it was auto-created in new topics based on that master page *AND* it generated data in Google. However, I never did much with it (beyond initially confirming that it worked). I did notice that the code for that older GA script is fairly different from the new GA script (see my initial post in this thread) I generated for my new RH2019 project. Here's the older script:
<?rh-region_start type="footer" style="width: 100%; position: relative;" ?>
ga('create', 'UA-#########', 'auto');
<?rh-region_end type="footer" ?>
I'll have to play around with these more.
Let us know how it goes. 🙂