magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Gtag function not defined in Magento >=2.4.5

Open cptX opened this issue 1 year ago • 7 comments

Preconditions and environment

  • Magento version 2.4.5

Steps to reproduce

Set up GA4 in Magento then try to include a gtag event on a button or something. I tried to call gtag function when a product is clicked in the autocomplete result list that comes from the search field though the elasticsuite autocomplete functionality. Gtag function was not recognised although my site successfully transmits data to google analytics. In console I get the following error "Uncaught ReferenceError: gtag is not defined"

Is gtag function implemented in the default core Magento - GA4 integration? Note that I haven't included any GA4 code neither on head or body or footer...

UPDATE: Maybe my mistake is that I haven't called the Magento Google Gtag module in my js file. What should I put here? image

Expected result

gtag function should be recognized and successfully transmit events.

Actual result

Now I receive the following error: "Uncaught ReferenceError: gtag is not defined"

Additional information

No response

Release note

No response

Triage and priority

  • [ ] Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • [ ] Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • [ ] Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • [X] Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • [ ] Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.

cptX avatar Jun 08 '23 05:06 cptX

Hi @cptX. Thank you for your report. To speed up processing of this issue, make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:


Join Magento Community Engineering Slack and ask your questions in #github channel. :warning: According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting. :clock10: You can find the schedule on the Magento Community Calendar page. :telephone_receiver: The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

m2-assistant[bot] avatar Jun 08 '23 05:06 m2-assistant[bot]

Hi @engcom-Bravo. Thank you for working on this issue. In order to make sure that issue has enough information and ready for development, please read and check the following instruction: :point_down:

  • [ ] 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).
  • [ ] 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue.
  • [ ] 3. Add Area: XXXXX label to the ticket, indicating the functional areas it may be related to.
  • [ ] 4. Verify that the issue is reproducible on 2.4-develop branch
    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!
  • [ ] 5. Add label Issue: Confirmed once verification is complete.
  • [ ] 6. Make sure that automatic system confirms that report has been added to the backlog.

m2-assistant[bot] avatar Jun 13 '23 09:06 m2-assistant[bot]

Hi @cptX,

Thank you for reporting and collaboration.

Verified the issue on Magento 2.4-develop instance and the issue is not reproducible.Kindly refer the screenshots.

To use Gtag function with your website, you have to include GA4 code in the header and footer. The configuration should be correct. We have created gtag purchase event and it is successfully transmitting data. Magento also transmits data to google analytics. We didn't find any console error. Might be you are missing some steps.

Steps to Setup:

  • Install Google Analytics Debugger Extension
  • Create GA4 Configuration Tag
  • Create any event (say purchase event) using tags inside google tag manager
  • Install google tag manager code in the header and footer of the site
  • Install Google Analytics Debugger Extension
  • Create GA4 Configuration Tag
  • Create any event (say purchase event) using tags inside google tag manager
  • Install google tag manager code in the header and footer of the site
Screenshot 2023-06-14 at 12 53 04 PM Screenshot 2023-06-14 at 12 56 00 PM Screenshot 2023-06-14 at 12 59 11 PM Screenshot 2023-06-14 at 12 59 37 PM

Gtag function is recognized and transmits the data successfully.

Kindly recheck the behaviour on Magento 2.4-develop instance and elaborate steps to reproduce if the issue is still reproducible.

Thanks.

engcom-Bravo avatar Jun 14 '23 07:06 engcom-Bravo

Hi, in my opinion we don't need to include extra GTAG code in header/footer as magento 2.4.5 includes it by itself, so putting it twice doesn't make sense!! My site already transmits to Google Analytics without putting anything in header and footer, so the code is already there!! What I'm probably missing is the library I need to include in my javascript in order to be able to use the gtag function. Any clues what I should add here: https://user-images.githubusercontent.com/9040042/244281899-e25ee26d-bcc3-42fa-a03a-0af26978730a.png

cptX avatar Jun 14 '23 08:06 cptX

Hi @cptX,

Thanks for your update.

This is not the correct platform to raise these kinds of queries,So we are suggesting you to please raise this query on Magento StackExchange https://magento.stackexchange.com/

Thanks.

engcom-Bravo avatar Jun 14 '23 11:06 engcom-Bravo

Hi @engcom-Bravo, first of all thanks for your time.

I would like to know from the designers of the system if actually do we need to put extra code in header and footer or not. Opinions are contradictory. In my opinion without using any extra code my site communicates correctly with GA, so it probably has GTAG somewhere implemented.

Now my question is how can we use this embedded functionality. The question was placed here with the hope the internal team could shed some light, because they know how it is implemented.

cptX avatar Jun 14 '23 11:06 cptX

Hi @cptX,

By referring this comment https://github.com/magento/magento2/issues/37592#issuecomment-1591009777 We are Considering this as a feature request to proceed further.

Thanks.

engcom-Bravo avatar Jan 10 '24 06:01 engcom-Bravo