Discussion:
[TYPO3-english] Google Analytics - insert javascript with page.headerData
Dennis Hoffland
2017-11-10 13:09:35 UTC
Permalink
Hello,

I have put the following code into the TS Setup of my root page:

# *****
# Google Analytics
# *****

temp.GoogleAnalytics = TEXT
temp.GoogleAnalytics.value (
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
ga('set', 'anonymizeIp', true); // Toelichting AP: Hiermee zet u de IP-maskering aan
gtag('config', 'UA-xxxxxxx-x');
</script>
)

page.headerData.50 = TEMPLATE
page.headerData.50.template < temp.GoogleAnalytics


The HTML output only contains:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>

The second section of the javascript is simply missing ...


If I try instead:

# *****
# Google Analytics
# *****

page.headerData.50 = TEXT
page.headerData.50.value (
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>
)
page.headerData.60 = TEXT
page.headerData.60.value (
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
ga('set', 'anonymizeIp', true); // Toelichting AP: Hiermee zet u de IP-maskering aan
gtag('config', 'UA-xxxxxxx-x');
</script>
)

It produces the same result:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>

Is there something wrong with the syntax that prevents the second javascript section from being rendered?

Kind regards,

Dennis
Christian Hackl
2017-11-10 21:06:34 UTC
Permalink
why you do this?

You can direkt in the headerdata include your JS Code

page {
headerData {
50 = TEXT
50.value (
<script>
// YOUR JS CODE
</script>
)
}
}
Dennis Hoffland
2017-11-12 12:45:54 UTC
Permalink
Hello Christian,

If I use the standard textbook code as you suggested and replace

<script>
// YOUR JS CODE
</script>

with

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
ga('set', 'anonymizeIp', true); // Toelichting AP: Hiermee zet u de IP-maskering aan
gtag('config', 'UA-xxxxxxx-x');
</script>

I get the same outcome as described in my first post:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-xxxxxxx-x"></script>

shows up in my HTML page header, and

<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
ga('set', 'anonymizeIp', true); // Toelichting AP: Hiermee zet u de IP-maskering aan
gtag('config', 'UA-xxxxxxx-x');
</script>

is somehow ignored.

My conclusion: my TS is in principle OK, however the (syntax? of the) second section of the javascript causes some processing issue.

Any thoughts?

Kind regards,

Dennis
Dennis Hoffland
2017-11-12 12:50:04 UTC
Permalink
Just to be clear: I am using TYPO3 CMS 7.6.22
Dennis Hoffland
2017-11-12 13:22:32 UTC
Permalink
Hello,

I did some further investigation.

I am using the scriptmerger extension.

For reasons not entirely clear to me the first section of my javascript turns up seperately in my page header, while the second section becomes part of the merged javascript (/typo3temp/scriptmerger/compressed/head-b15c2d1e76d0f77c7d3249fcc4d6ca05.merged.gz.js). Although it seems to "disappear" in the source code, the second javascript section is in fact now part of the merged javascript.

Mistery solved!

Kind regards,

Dennis
Christian Hackl
2017-11-12 19:36:34 UTC
Permalink
hmm ....

you can try, to seperate it:

50 = TEXT
.. first

51 = TEXT
.. second


But it shoold be no difference in the output. hmmmm...

Loading...