Adding Google Tag Manager Code to the WebHelp output
Post here questions and problems related to editing and publishing DITA content.
Adding Google Tag Manager Code to the WebHelp output
Post by Anonymous1 »
Hello,
we would like to add the Google Tag Manager code to our WebHelp. The instructions of Google name the following requirements:
I thought this is going to be pretty easy. But I have some issues getting it to work.
We are using Oxygen 21.1 with a custom framework. The WebHelp Responsive plug-in is the default one though. I created a publishing template to add the Google Tag code to the WebHelp output.
This gives me the following results:
we would like to add the Google Tag Manager code to our WebHelp. The instructions of Google name the following requirements:
- Copy the following JavaScript and paste it as close to the opening <head> tag as possible on every page of your website, replacing GTM-XXXX with your container ID.
- Copy the following snippet and paste it immediately after the opening <body> tag on every page of your website, replacing GTM-XXXX with your container ID.
I thought this is going to be pretty easy. But I have some issues getting it to work.
We are using Oxygen 21.1 with a custom framework. The WebHelp Responsive plug-in is the default one though. I created a publishing template to add the Google Tag code to the WebHelp output.
Code: Select all
<html-fragments>
<fragment file="fragments/google_tag_header.html" placeholder="webhelp.fragment.head"/>
<fragment file="fragments/google_tag.html" placeholder="webhelp.fragment.before.body"/>
</html-fragments>
- The Google Tag code for the body is added somewhere in the body. Not immediately after the opening <body> tag but shortly after.
- The Google Tag code for the head does not appear at all in the output.
- Am I using the wrong code in the HTML file for the head code? Why does it not show up in the output?
- Is it possible to put the code for the body right after the opening <body> tag?
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-123');</script>
<!-- End Google Tag Manager -->
-
- Posts: 404
- Joined: Thu Aug 21, 2003 11:36 am
- Location: Craiova
- Contact:
Re: Adding Google Tag Manager Code to the WebHelp output
Post by radu_pisoi »
Hi,
You can read more in the How to Insert HTML Content in WebHelp Responsive Output topic from our documentation.
The file content referenced using a webhelp.fragment.* parameter should be XML valid. Also, if you have special characters (for example, &, <) that break the well-formedness of the XML fragment, it is important to place this content inside an XML comment.
Code: Select all
<html>
<!-- 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-123'); --></script>
<!-- End Google Tag Manager -->
</html>
You can do this only by editing the HTML Page Layout Files used in the publishing template. Open the template files and move the webhelp.fragment.before.body fragment to the desired location.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 152
- Joined: Fri Apr 24, 2015 12:28 pm
Re: Adding Google Tag Manager Code to the WebHelp output
Hi Radu,
You write the following:
Thanks,
Christina
You write the following:
Do you mean that the original HTML Page Layout Files from the WebHelp Responsive plugin should be edited? Or can you define your own page templates that override the original files?You can do this only by editing the HTML Page Layout Files used in the publishing template. Open the template files and move the webhelp.fragment.before.body fragment to the desired location.
Thanks,
Christina
oXygen XML Editor 27.1 build 2025041508
DITA OT 3.7.4
DITA OT 3.7.4
-
- Posts: 404
- Joined: Thu Aug 21, 2003 11:36 am
- Location: Craiova
- Contact:
Re: Adding Google Tag Manager Code to the WebHelp output
Post by radu_pisoi »
Hi,
The location of HTML Page Layout files is DITA-OT-DIR/plugins/com.oxygenxml.webhelp.responsive/oxygen-webhelp/page-templates.
After you copy them, update the publishing template descriptior file to specify the template files:
No, do not edit the original HTML Page Layout files, copy them inside your publishing template archive/folder. You can put them in the page-templates folder for instance.
The location of HTML Page Layout files is DITA-OT-DIR/plugins/com.oxygenxml.webhelp.responsive/oxygen-webhelp/page-templates.
After you copy them, update the publishing template descriptior file to specify the template files:
Code: Select all
<publishing-template>
...
<webhelp>
...
<!-- HTML page layout files -->
<html-page-layout-files>
<page-layout-file page="main" file="page-templates/wt_index.html"/>
<page-layout-file page="search" file="page-templates/wt_search.html"/>
<page-layout-file page="topic" file="page-templates/wt_topic.html"/>
<page-layout-file page="index-terms" file="page-templates/wt_terms.html"/>
</html-page-layout-files>
...
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 152
- Joined: Fri Apr 24, 2015 12:28 pm
Re: Adding Google Tag Manager Code to the WebHelp output
Hi Radu,
I've managed somehow to get the Google Tag Manager code in the correct location, but when I run the transformation, the XML comment does not disappear - which it should, shouldn't it? Is there anything else that we need to set/configure to make it transform properly?
This is the content of the file:
And this is what's added to the <head>-element in the html output:
Thanks,
Christina
I've managed somehow to get the Google Tag Manager code in the correct location, but when I run the transformation, the XML comment does not disappear - which it should, shouldn't it? Is there anything else that we need to set/configure to make it transform properly?
This is the content of the file:
Code: Select all
<html>
<!-- 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-123'); --></script>
<!-- End Google Tag Manager -->
</html>
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-123'); --></script>
<!-- End Google Tag Manager -->
Christina
oXygen XML Editor 27.1 build 2025041508
DITA OT 3.7.4
DITA OT 3.7.4
-
- Posts: 404
- Joined: Thu Aug 21, 2003 11:36 am
- Location: Craiova
- Contact:
Re: Adding Google Tag Manager Code to the WebHelp output
Post by radu_pisoi »
Hi,
We parse all files referenced through a webhelp.fragment.* parameter as XML files. So, they are required to be XML valid.
We parse all files referenced through a webhelp.fragment.* parameter as XML files. So, they are required to be XML valid.
No, the XML comment inside the script should be preserved. The comment is transparent for the browser, so the Javascript code inside the comment will be executed. Is it not your case?
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
-
- Posts: 152
- Joined: Fri Apr 24, 2015 12:28 pm
Re: Adding Google Tag Manager Code to the WebHelp output
Hi Radu,
I've managed to run extensive tests now with the Google Tag Manager script. The result is that it is VERY important where you put the XML comment tags.
It is not sufficient to just add them in front of and at the end of the script content. You must also make sure that the script content does not start or end on the same line as the XML comment tags. This means that my code must look as follows (I've also added type="text/javascript", just in case):
It is also not sufficient to just add a space between the XML comment tags and the script content. It will not work.
I found that there was a Javascript error, telling you
This is a weird error, because the colon belongs to the script. Only Firefox will reveal visually that something's wrong with the XML comment. Google Chrome will not help you - at least it didn't help me.
I would suggest that this tiny piece of information should be mentioned on the documentation page that you linked somewhere in the thread.
It didn't occur to me that there could be an issue with this.
Best,
Christina
I've managed to run extensive tests now with the Google Tag Manager script. The result is that it is VERY important where you put the XML comment tags.
It is not sufficient to just add them in front of and at the end of the script content. You must also make sure that the script content does not start or end on the same line as the XML comment tags. This means that my code must look as follows (I've also added type="text/javascript", just in case):
Code: Select all
<html>
<!-- Google Tag Manager -->
<script type="text/javascript"><!--
(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-123456');
--></script>
<!-- End Google Tag Manager -->
</html>
I found that there was a Javascript error, telling you
Uncaught SyntaxError: Unexpected token ':'
This is a weird error, because the colon belongs to the script. Only Firefox will reveal visually that something's wrong with the XML comment. Google Chrome will not help you - at least it didn't help me.
I would suggest that this tiny piece of information should be mentioned on the documentation page that you linked somewhere in the thread.
It didn't occur to me that there could be an issue with this.
Best,
Christina
oXygen XML Editor 27.1 build 2025041508
DITA OT 3.7.4
DITA OT 3.7.4
-
- Posts: 404
- Joined: Thu Aug 21, 2003 11:36 am
- Location: Craiova
- Contact:
Re: Adding Google Tag Manager Code to the WebHelp output
Post by radu_pisoi »
Hi,
Thank you for your suggestion!
I will add a task to improve documentation.
Thank you for your suggestion!
I will add a task to improve documentation.
Radu Pisoi
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
<oXygen/> XML Editor, Schema Editor and XSLT Editor/Debugger
http://www.oxygenxml.com
Return to “DITA (Editing and Publishing DITA Content)”
Jump to
- Oxygen XML Editor/Author/Developer
- ↳ Feature Request
- ↳ Common Problems
- ↳ DITA (Editing and Publishing DITA Content)
- ↳ SDK-API, Frameworks - Document Types
- ↳ DocBook
- ↳ TEI
- ↳ XHTML
- ↳ Other Issues
- Oxygen XML Web Author
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Content Fusion
- ↳ Feature Request
- ↳ Common Problems
- Oxygen JSON Editor
- ↳ Feature Request
- ↳ Common Problems
- Oxygen PDF Chemistry
- ↳ Feature Request
- ↳ Common Problems
- Oxygen Feedback
- ↳ Feature Request
- ↳ Common Problems
- Oxygen XML WebHelp
- ↳ Feature Request
- ↳ Common Problems
- XML
- ↳ General XML Questions
- ↳ XSLT and FOP
- ↳ XML Schemas
- ↳ XQuery
- NVDL
- ↳ General NVDL Issues
- ↳ oNVDL Related Issues
- XML Services Market
- ↳ Offer a Service