I'm not hugely familiar with Magento 2. I have Google Analytics set up through Magento 2 by going through the settings:

Stores > Configuration > Sales > Google API

But there is no option for cross domain tracking.

When I have looked up online, I just find articles about e-commerce tracking or, how to do cross domain tracking through Google Tag Manager. I need to know how to do it through Magento 2.

I'm assuming we're going to have to access the file that houses the GA code because I don't see the option in the Magento UI, I wanted to check if there was another way before going down this route.

  • I think for that you can do the custom extension and added code into that one. Aug 20, 2020 at 11:37

1 Answer 1


So in Magento2 you have GA module with javascript file: /vendor/magento/module-google-analytics/view/frontend/web/js/google-analytics.js

part of file:

        // Process page info
        ga('create', config.pageTrackingData.accountId, 'auto');

        if (config.pageTrackingData.isAnonymizedIpActive) {
            ga('set', 'anonymizeIp', true);

You add here extra info required by GA in this guide: https://support.google.com/analytics/answer/1034342?hl=en

  ga('create', 'UA-XXXXXXX-Y', 'auto', {'allowLinker': true});
  ga('require', 'linker');
  ga('linker:autoLink', ['example-2.com']);

Depending on your time and goals you can do it in few ways:

1) override this file by adding it to your Magento2 theme

vendor/[namespace]/[module_name]/Magento_GoogleAnalytics/web/js/google-analytics.js and adding required changes.

This is not usually the recommend way. However in this case looking how this JS is build it wouldn't be so bad. Just remember in case of Magento 2 update any changes to original function would need to be moved to this file.

2) using a mixin

It requires understanding how JS system works in Magento 2 (requirejs, etc). You can follow the guide here about how to use mixins: https://devdocs.magento.com/guides/v2.4/javascript-dev-guide/javascript/js_mixins.html

Proper example from guide to your case is "Extend JS function".

Using mixin is usually my default way. However sometimes depending on use case (to avoid extra js request and/or a mixin change so little or too much I decide to use first method).

3) Create a module with some configuration option in admin panel

This way if it need to be configurable.

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.