2
  • Though Magento 2 has the inbuilt implementation to send the transaction/order details placed on webstore or frontend to Google Analytics, it lacks the same ability when orders are being placed from the Magento backend, the admin, or when orders are created programmatically.
  • The problem Google Analytics won't match the reports generated from Magento.
  • So I want to send order to google save Google Analytics Tracking Id.

How can I achieve this?

3
  • 1
    what you have tried?
    – Amit Bera
    Commented Mar 28, 2019 at 8:01
  • 1
    i have not tried,even dont know how to do it Commented Mar 28, 2019 at 8:51
  • Hello @KetanBorada, did you try my solution ?
    – Pawan
    Commented Apr 29, 2019 at 17:08

1 Answer 1

1

I have checked and found that module-google-analytics is responsible for sending order analytics to Google.

It has events.xml at magento2Root/vendor/magento/module-google-analytics/etc/frontend with following content.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="checkout_onepage_controller_success_action">
        <observer name="googleanalytics_order_success" instance="Magento\GoogleAnalytics\Observer\SetGoogleAnalyticsOnOrderSuccessPageViewObserver" />
    </event>
    <event name="multishipping_checkout_controller_success_action">
        <observer name="googleanalytics_order_success" instance="Magento\GoogleAnalytics\Observer\SetGoogleAnalyticsOnOrderSuccessPageViewObserver" />
    </event>
</config>

So, it is calling observer on order success but only for front-end.

If you move this file form frontend to global, it will work for admin as well!

So new location for events.xml will be:

magento2Root/vendor/magento/module-google-analytics/etc/events.xml

Note:

If above observer not work, you can also use checkout_submit_all_after, OR sales_order_place_after.

I have not tested above code due to unavailability of key.

If worked, do proper override in custom module.

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.