Need a bit of help creating a hook for Analytics latest script

Open to all! Ask other shopowners for help.
14Steve14
Senior Contributor
Posts: 920
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Need a bit of help creating a hook for Analytics latest script

Post 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.
Last edited by 14Steve14 on Thu Sep 19, 2024 5:38 pm, edited 1 time in total.


Join The Code Co-op to get access to your library in the Code Co-op Forum
User avatar
burt
Core Team
Posts: 4546
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Need a bit of help creating a hook for Analytics latest script

Post 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.
I am not here to build for you.
I am here to build with you. Let's help each other.
Omar_one
Senior Contributor
Posts: 676
Joined: Fri Oct 25, 2019 5:06 pm
Phoenix Version: v1.0.8.16
Has thanked: 100 times
Been thanked: 56 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by Omar_one »

we are using module created by @zipurman
app.php/addons/paid_addon/google_analytics_4
14Steve14
Senior Contributor
Posts: 920
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Need a bit of help creating a hook for Analytics latest script

Post 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;
  }
}
14Steve14
Senior Contributor
Posts: 920
Joined: Fri Oct 25, 2019 7:01 pm
Phoenix Version: v1.0.9.1
Has thanked: 17 times
Been thanked: 103 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by 14Steve14 »

Omar_one wrote: Thu Sep 19, 2024 5:12 pm we are using module created by @zipurman
app.php/addons/paid_addon/google_analytics_4
I used that module but it was not showing the latest data using the google tag.
User avatar
burt
Core Team
Posts: 4546
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by burt »

I am not here to build for you.
I am here to build with you. Let's help each other.
User avatar
zipurman
Builder
Posts: 540
Joined: Tue Oct 13, 2020 5:20 pm
Phoenix Version: v
Has thanked: 93 times
Been thanked: 162 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by zipurman »

14Steve14 wrote: Thu Sep 19, 2024 5:41 pm
Omar_one wrote: Thu Sep 19, 2024 5:12 pm we are using module created by @zipurman
app.php/addons/paid_addon/google_analytics_4
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.
zipurman
-----------
frankl
Builder
Posts: 159
Joined: Tue Feb 23, 2021 8:39 pm
Phoenix Version: v1.1.0.4
Has thanked: 17 times
Been thanked: 25 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by frankl »

First thing I would do is try using

<<<EOD

rather than

<<<gtag

Not tested
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Need a bit of help creating a hook for Analytics latest script

Post 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.
User avatar
burt
Core Team
Posts: 4546
Joined: Tue Oct 29, 2019 9:37 am
Phoenix Version: v1.1.0.8
Has thanked: 252 times
Been thanked: 412 times

Re: Need a bit of help creating a hook for Analytics latest script

Post by burt »

I learned something new today ! Cheers guys
I am not here to build for you.
I am here to build with you. Let's help each other.


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply