Magento2: Magento 2.2.5: Google Analytics not added to head correctly

Created on 3 Jul 2018  路  35Comments  路  Source: magento/magento2

Preconditions

  1. Magento version 2.2.5
  2. PHP 7.1
  3. MariaDB 15.1
  4. NginX 1.13.8

Steps to reproduce

  1. Enable Google Analytics trough admin Stores -> Configuration -> Sales -> Google API
  2. Fill in account number
  3. Save configuration
  4. Clear cache

Expected result

  1. Google Analytics JavaScript should be loaded on the frontend

Actual result

  1. Google Analytics isn't added at all on the frontend.

I managed to find the problem myself. In Magento 2.2.5 the file Magento_GoogleAnalytics/view/frontend/layout/default.xml was changed.

It now injects the Google Analytics block trough:
<referenceContainer name="head.additional">

But head.additional isn't a container but a block.

The fix I am using is to create a file:
app/design/frontend/-THEME VENDOR-/-THEME NAME-/Magento_GoogleAnalytics/layout/override/base/default.xml

with following content:

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="head.additional"> <block class="Magento\GoogleAnalytics\Block\Ga" name="google_analytics" as="google_analytics" template="Magento_GoogleAnalytics::ga.phtml"/> </referenceContainer> </body> </page>

GoogleAnalytics mm18hr Fixed in 2.2.x Fixed in 2.3.x Clear Description Confirmed Format is valid Ready for Work Reproduced on 2.2.x Reproduced on 2.3.x

All 35 comments

Hi @WalterSmulders. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • [ ] Summary of the issue
  • [ ] Information on your environment
  • [ ] Steps to reproduce
  • [ ] Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento-engcom-team give me {$VERSION} instance

where {$VERSION} is version tags (starting from 2.2.0+) or develop branches (2.2-develop +).
For more details, please, review the Magento Contributor Assistant documentation.

@WalterSmulders do you confirm that you was able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • [ ] yes
  • [ ] no

@WalterSmulders, thank you for your report. I've checked this issue on Magento 2.2.5, and google-analytics.js loads for me on storefront just fine.
source
js

Adding link to the Pull Request where this was introduced: https://github.com/magento/magento2/pull/14293

@WalterSmulders, we are closing this issue due to inactivity. If you'd like to update it, please reopen the issue.

Did this issue ever get resolved as i'm having the same issue?

Probably not. So before this it was a block and now it is a container?

This would be a breaking change which affects all who overwrite default.xml (which is the case when you create a custom theme or extend theme files).

@DanielRuf Thanks. I think the default theme has changed, i've inherited a project, and updated it from 2.1.8 to 2.2.5 do you know what the fix is for this?

The fix for us who have a custom or other theme based on Blank / Luma have to port the changes to the overwritten / changed files. Good to know, will have to check this also for one of our projects.

Basically doing a diff (changing block to container).

We have to check here if 2.2.5 or a previous release introduced this change.

Hi @DanielRuf seem this issue was fixed in 2.2.5 but if you have a custom theme you need overwrite odefault.xml to origin luma or blank file ?
Hi @eamontaylor Did you solve your problem ?

Hi I've updated the following default.xml file it's not working so i guess it's not the right path.

app/design/frontend/-THEME VENDOR-/-THEME NAME-/Magento_Theme/layout/default.xml

<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="head.additional"> <block class="Magento\GoogleAnalytics\Block\Ga" name="google_analytics" as="google_analytics" template="Magento_GoogleAnalytics::ga.phtml"/> </referenceContainer> <referenceBlock name="reviews.tab" remove="true" /> <referenceBlock name="product.review.form" remove="true" /> <referenceBlock name="product.info.review" remove="true" /> <referenceBlock name="category.product.addto" remove="true" /> <referenceBlock name="related.post.tab" remove="true" /> </body> </page>

What file exactly do I need to update?

@eamontaylor this is right path -> app/design/frontend/-THEME VENDOR-/-THEME NAME-/Magento_GoogleAnalytics/layout/override/base/default.xml

@engcom-backlog-nazar nope this doesn't work for me.

Upgraded, Compiled, Deployed, Cleared cache and still nothing,

@eamontaylor did you clear pub static files?

@eamontaylor If you correctly override the XML it will work, I have this fix in place in several live Magento installs

@eamontaylor you may do something wrong, i do this with custom theme and it works perfectly.

@DanielRuf Can i close issue ? or leave open, until somebody change name from container to block ?

Do i need to change refernceContainer to referenceBlock?

@eamontaylor yes but seems it's not solve your problem.

@engcom-backlog-nazar Thanks, i've tried everything you've suggested but still no joy.

Did you clear all caches?

@DanielRuf Yep cleared the caches again and that seems to of sorted it thanks everyone.

I believe @WalterSmulders has a point here.

1) head.additional is created as a Block
2) Is being referenced correctly in the catalog module
3) Is being incorrectly referenced as a container in the google analytics module

The strange thing is, that this seems to work perfectly fine. So you can reference a block using referenceContainer which is not what I expected.

I'm not sure if customisations can break this somehow, but I think it would be best that the referenceContainer is replaced with referenceBlock in the google analytics module.

@WalterSmulders , thank you for your report.
We've acknowledged the issue and added to our backlog.

@engcom-backlog-nazar happy to hear the bug is acknowledged and it will be fixed

mm18hr

Hi @WalterSmulders. Thank you for your report.
The issue has been fixed in magento/magento2#18290 by @sambolek in 2.3-develop branch
Related commit(s):

The fix will be available with the upcoming 2.3.1 release.

Hi @WalterSmulders. Thank you for your report.
The issue has been fixed in magento/magento2#18375 by @sambolek in 2.2-develop branch
Related commit(s):

The fix will be available with the upcoming 2.2.8 release.

I am on 2.2.7. I experience the exact same issue with the Adwords block (... while the Analytics block is working for me). Any comments on that?

@WalterSmulders can you please make a comment on this?

@WalterSmulders can you please make a comment on this?

I did not experience this issue. I recommend to see if the fix I gave in the opening post can be applied to the Adwords block, furthermore I think it's wise to treat this as a separate issue since this one is closed and likely contains a other part of the code.

@WalterSmulders ok thanks I wil investigate and post a new issue if necessary

The Google Analytics code is still not being properly added to the head in 2.2.8. I applied the fix in 2.2.7 as a layout extension - theme-dir/Magento_GoogleAnalytics/layout/default and it worked properly in 2.2.7 after it was applied. I upgraded to 2.2.8 and removed the layout customization, but now the code is not being added properly as it was before. Also, I have cleared all my caches but that hasn鈥檛 solved my issue.

I resolved my issue. It was due to another layout extension I had within theme-dir/Magento_Catalog/layout/default.xml. I didn't realize that the latest fix in 2.2.8 referenced that.

Was this page helpful?
0 / 5 - 0 ratings