Magento – Magento2 send Order information to Google Analytics when order from admin

adminevent-observergoogle analyticsmagento2orders

  • 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?

Best Answer

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.