Page 1 of 2
Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 3:35 pm
by 14Steve14
I have tried twice to copy and adjust a hook file to add in the latest Google tag and failed for some reason. Google seems unable to find the tag. If I add the tag directly into templates/override/components/template_top.php google can find the tag.
The tag needs to do in between the <head></head> tags
Starting right from the beginning where is the best place to add the following code.
Code: Select all
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=xxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxxxxx');
</script>
Can someone point me in the right direction please.
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 4:26 pm
by burt
listen_injectSiteStart
would seem to be a good one.
Therefore;
Code: Select all
<?php
// save this file at /includes/hooks/shop/siteWide/gtag.php
class hook_shop_siteWide_gtag {
function listen_injectSiteStart() {
$gtag = <<<EOJS
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-xxxxxx"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-xxxxxx');
</script>
EOJS;
return $gtag;
}
}
You would want to change G-xxxxxx appropriately.
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 5:12 pm
by Omar_one
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 5:38 pm
by 14Steve14
Thanks Gary.
With the supplied code I am now getting the following error in the store error logs.
[19-Sep-2024 18:20:12 Europe/London] PHP Parse error: Invalid body indentation level (expecting an indentation level of at least 2) in /home/####/####/includes/hooks/shop/siteWide/gtag.php on line 17
The error is breaking the website and showing a white page and a page not working error.
Line 17 as in the code supplied is
<!-- Google tag (gtag.js) -->
I read online that there should be no indent, which there isn't so I am totally confused by the error that says there should be.
Here is the code that I now have
Code: Select all
class hook_shop_siteWide_gtag {
function listen_injectSiteStart() {
$gtag = <<<gtag
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-###"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '###');
</script>
gtag;
return $gtag;
}
}
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 5:41 pm
by 14Steve14
I used that module but it was not showing the latest data using the google tag.
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 5:46 pm
by burt
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Thu Sep 19, 2024 9:49 pm
by zipurman
14Steve14 wrote: ↑Thu Sep 19, 2024 5:41 pm
I used that module but it was not showing the latest data using the google tag.
PM me and let me know what issues you are having and I can have a look. Many are using this without issues.
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Fri Sep 20, 2024 6:19 am
by frankl
First thing I would do is try using
<<<EOD
rather than
<<<gtag
Not tested
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Sat Sep 21, 2024 3:16 am
by ecartz
Yes, the <<<name syntax should be used only with names that do NOT appear in the quoted string. In this case, gtag appears in the quoted string so you can't use <<<gtag
That's why they tend to be nonsense strings EOS or EOJS -- because those are unlikely to appear in the string you're quoting.
Re: Need a bit of help creating a hook for Analytics latest script
Posted: Sat Sep 21, 2024 9:24 am
by burt
I learned something new today ! Cheers guys