3

I want to configure Google Analytics 4 in my Magento 2.4.4-p2 version.

Is there any way to configure it in Magento 2.4.4-p2?

I know Magento already provides this functionality in 2.4.6 but I want to configure it in 2.4.4-p2.

Here is the script for Google Analytics:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-DNJN1PF3CS"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());

gtag('config', 'G-XXXXXXX');
</script>

I'm looking for this type of script for Google Analytics 4

I don't want to use 3rd party Extension, Also I review the Magento2.4.6 code and Found There is New Module Magento_GoogleGtag For Google Analytics 4.

5 Answers 5

2

To add the Google Analytics (gtag.js) script to every page in Magento 2, you typically want to insert it in the HTML head section of your website. Here are the general steps to add the script to all pages in Magento 2:

- Edit the Theme Files:

Log in to the Magento 2 Admin Panel. Navigate to Content > Design > Configuration. Select the theme you are using (typically the default theme).

- Edit HTML Head:

Under the selected theme, go to the "HTML Head" section. In the "Scripts and Style Sheets" field, you can add your Google Analytics script.

- Insert the Script:

Insert the following code into the "Scripts and Style Sheets" field:

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
</script>

- Save Configuration:

Save the configuration changes.

- Flush Cache:

After making changes, it's a good practice to flush the cache. You can do this by navigating to System > Cache Management and clicking "Flush Magento Cache."

Please note that the exact steps and locations might vary slightly depending on your Magento version and the specific theme you are using. If you're using a custom theme, you may need to work directly with your theme files.

Additionally, consider using Google Tag Manager (GTM) for managing various tracking tags, including Google Analytics 4. GTM provides a user-friendly interface to manage scripts without directly modifying the Magento codebase. If you are not already using GTM, you might want to explore this option for easier tag management. This block post could help How to Setup Magento 2 GA4 (Google Analytics 4) with GTM?

1

There is a free module available by Yireo that supports Google Analytics 4. It supports versions 2.3.7 or higher and 2.4.1 or higher.

This module is a great module and highly recommended by the Magento community. You can take a look at: https://github.com/yireo/Yireo_GoogleTagManager2

2
  • I don't want to use 3rd Party module I have updated the answer can you please check. ? Commented Jun 20, 2023 at 12:40
  • @RonakRathod I didn't need to add GA4 manually like what you mentioned. However, I still strongly recommend using Yireo Google Tag Manager for several reasons. Firstly, it was developed by a Magento master with high-quality code. Secondly, it has contributions from well-known contributors in the Magento community. Thirdly, since it is open source and available on GitHub, you can view their code directly.
    – Tu Van
    Commented Jun 20, 2023 at 15:04
1

As you may already be aware, Magento provides Google Analytics 4 integration starting from Magento 2.4.6. However, there are two ways to achieve this

integration:

Recommended method:

  1. Utilize a third-party extension that is available for all Magento extensions. Personally, I use the WeltPixel GA4 extension, which can be found at the following link: https://www.weltpixel.com/google-analytics-enhanced-ecommerce-tag-manager-magento-2.html

Not Recommended method:

  1. Extract the extension code from the vendor folder of Magento 2.4.6 and place it in your Magento setup's app/code directory, ensuring that you include the required libraries.

To identify the necessary libraries and basic requirements, refer to the composer.json file of the Magento Google Analytics extension.

I hope this clarifies the steps required for integrating Google Analytics 4 into Magento 2.4.4

4
  • I don't want to use 3rd Party module I have updated the answer can you please check. ? Commented Jun 20, 2023 at 12:40
  • @RonakRathod then try second method from my answer, I think that will works for you. Commented Jun 20, 2023 at 12:42
  • Yes try this but there is one option that is missing "Enable Content Experiments" and that field is rendered on "Magento_GoogleOptimizer" module so there is a chance to miss something. Commented Jun 20, 2023 at 12:53
  • @RonakRathod You need to take below modules, Magento_GoogleAdwords, Magento_GoogleAnalytics, Magento_GoogleGtag, Magento_GoogleOptimizer Commented Jun 20, 2023 at 12:59
1

I checked and found it on the Google Analytics Platform just i need to add this script to Every page

<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-XXXXXXXXXX');
</script>

enter image description here

1
0

Although Magento does provide the GA4 functionality it's not that extended. You still need to connect GTM separately and create all the events there.

So I'd recommend using the ready-made Magefan GTM Extension for Magento. It come switch the pre-built GTM tags, triggers and variables. All you need is just tweak a few options and the import the generated JSON file to your GTM account.

It's super easy to install and use. Here's the commands you can use to install it:

composer require magefan/module-google-tag-manager php bin/magento setup:upgrade php bin/magento setup:di:compile php bin/magento setup:static-content:deploy

New contributor
Magefan is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.