cancel
Showing results for 
Search instead for 
Did you mean: 

Setting up Google Analytics in Magento 2

SOLVED

Setting up Google Analytics in Magento 2

Hi,

 

We are really struggling setting up google analytics on our site at https://kilmartinglenspirits.co.uk

We added the tag into our magento site via Stores>Configuration>Sales>Google API

When to went to test it in Google analytics Real Time the tag was firing for mobile users but nothing for laptop users. After talking to the guys at Google analytics help they suggested recreating the tag which we did and reinstalled the tag on our site.

We then got desktop users being captured but not the mobile user !!!

We came back on the computer later and now the tag is not being recognized by the chrome tag manager extension and we are now getting nothing reported by google analytics

 

If we view the source we can see Magento has added the code as follows
<script type="text/x-magento-init">
{
"*": {
"Magento_GoogleAnalytics/js/google-analytics": {
"isCookieRestrictionModeEnabled": 1,
"currentWebsite": 1,
"cookieName": "user_allowed_save_cookie",
"ordersTrackingData": [],
"pageTrackingData": {"optPageUrl":"","isAnonymizedIpActive":false,"accountId":"UA-21179xxxx-1"} }
}
}
</script>

 

We are currently on Magento ver. 2.3.6-p1

Any help would rally be appreciated as we are really struggling to solve this issue

 

Thanks

Rich

2 ACCEPTED SOLUTIONS

Accepted Solutions

Re: Setting up Google Analytics in Magento 2

Go to "content">"design">"configuration">

You see a table which shows the websites, stores and storeviews that have been setup. Probably it;'s best to choose the top option (press 'edit'), which is setting this global for the shop. You now will see: "scope: Global", Default Theme and Design Rule. In that section there is a tab 'HTML Head' click on that and a form appears. You can also set a favicon page title, meta tags and keywords there and somewhere under there is a field called "Scripts and Style Sheets" you can place the Analytics code there. save the configuration and see if it's now working.

 

that would be something like this code you should add there (this is an example code):

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

View solution in original post

Re: Setting up Google Analytics in Magento 2

Hi

Thanks for your reply

 

I've tried this and it works brilliantly

I was under the impression I should enter the GA code (UA-xxxxxx-x) in the Google API which I have used before but on this site it wouldn't work after several attempts at recreating and re-adding

 

Cheers

Rich

 

 

View solution in original post

2 REPLIES 2

Re: Setting up Google Analytics in Magento 2

Go to "content">"design">"configuration">

You see a table which shows the websites, stores and storeviews that have been setup. Probably it;'s best to choose the top option (press 'edit'), which is setting this global for the shop. You now will see: "scope: Global", Default Theme and Design Rule. In that section there is a tab 'HTML Head' click on that and a form appears. You can also set a favicon page title, meta tags and keywords there and somewhere under there is a field called "Scripts and Style Sheets" you can place the Analytics code there. save the configuration and see if it's now working.

 

that would be something like this code you should add there (this is an example code):

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->

View solution in original post

Re: Setting up Google Analytics in Magento 2

Hi

Thanks for your reply

 

I've tried this and it works brilliantly

I was under the impression I should enter the GA code (UA-xxxxxx-x) in the Google API which I have used before but on this site it wouldn't work after several attempts at recreating and re-adding

 

Cheers

Rich

 

 

View solution in original post