Archived topic
Delay Execution of Google Analytics
14 replies · Started by Kathrin on March 13, 2021
I have added the Google Analytics tracking code via hooks to GeneratePress. As I saw there is the possibility to change the priority? Should I change here something when I want that Google Analytics is executed after the site rendering has finished? In the past I always used Flying Scripts to execute Google Analytics with a delay of 3 seconds. That worked very well for my purposes, but does that also work with the hook setting priority 10?
Greetings Kathrin
Hi there,
Priority is for when there are multiple elements added to the same hook and you can set the order of execution so it wouldn't work for what you want.
Where are you adding the code? in wp_head? is so it will load before the content.
Yes, I added to wp_head? Ok and I can't delay it with Flying Scripts?
Greetings Kathrin
Hi there,
you add the Script to WP_head hook. You can leave the priority at default.
Then in Flying scripts you delay the loading of the Javascript file that will be requested - which should be called: analytics.js
Dear David, thank you very much! Than it works as I have expected :)
Greetings Kathrin
I added the following code to the hook, but it doesn't work :( The delay works as expected, but not Google Analytics itself.
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=XXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'XXXXXXX' {'anonymize_ip': true});
</script>
Greetings Kathrin
So does the code works without the delay?
No, the code doesn't work. It seems as the code isn't recognized... :(
I get this message:
Dieses Element benötigt einen in den Anzeigeregeln festgelegten Anzeigeort, um sichtbar zu sein.
The element needs determined place in display settings to be shown.
What does that mean and what should I do?
Greetings Kathrin
In the Element you should see Display Rules:
https://docs.generatepress.com/article/hooks-element-overview/#display-rules
Set the Location to Entire Site
Dear David, I have already done that, but there is still no change. It seems that the code isn't recognized by the system.
Greetings Kathrin
I checked your site and i can see a link for Google Tag Manager in the <head>
GTM requires 2 codes to be added... this document explains how:
https://docs.generatepress.com/article/implement-google-tag-manager/
Ok, but I just want to add Google Analytics. So to get that work I have to add the two GTag Codes and the one for Google Analytics? But I don't have a Google Tag Manager ID just one for Google Analytics. Or is it the same?
Greetings Kathrin
This depends on how you have setup your Google Analytics. If you're not using GTM then you should just have a single code like the example they provide here:
https://developers.google.com/analytics/devguides/collection/analyticsjs
For that code you ONLY need to add the one code to the wp_head hook
Hi David, it works no, I forgot to set a comma when anonymizing :) It works when it is set like this:
<!– Global site tag (gtag.js) – Google Analytics –>
<script async src=”https://www.googletagmanager.com/gtag/js?id=XXXXXXXX”></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag(‘js’, new Date());
gtag(‘config’, ‘XXXXXXX’, {‘anonymize_ip’: true});
</script>
Greetings Kathrin
Glad to hear you got it working.