---
title: Tracking Magento 2 backend orders in Google Analytics using Measurement Protocol
description: e-Commerce tracking is of immense importance and a significant number of merchants use Google Analytics for tracking all the transactions or order details. 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.
image: https://blog.e-zest.com/hubfs/blog-files/Tracking%20Magento%202%20backend%20orders%20in%20Google%20Analytics%20using%20Measurement%20Protocol.png
---

[![e-Zest logo\_An accion lab company\_Colour](https://blog.e-zest.com/hubfs/images/Logos%20with%20Tagline/Corporate/SVG/e-Zest%20logo_An%20accion%20lab%20company_Colour.svg "e-Zest logo_An accion lab company_Colour")](https://www.e-zest.com/)

- [WORK](https://www.e-zest.com/our-work)
- [SERVICES](https://www.e-zest.com/our-services) 
    - [Product Engineering](https://www.e-zest.com/digital-product-engineering-services)
    - [Enterprise Data Services](https://www.e-zest.com/enterprise-data-services)
    - [Digital Commerce](https://www.e-zest.com/digital-commerce-services)
    - [Digital Operations](https://www.e-zest.com/digital-operations)
    - [Digital Consulting](https://www.e-zest.com/digital-transformation-consulting-services)
    - [Digital Experience Design](https://www.e-zest.com/digital-experience-design)
- [INSIGHTS](https://blog.e-zest.com/)
- [CAREERS](https://www.e-zest.com/careers)
- [ENTERPRISE DATA SERVICES](https://www.e-zest.com/enterprise-data-services)
- [GENERATIVE AI](https://www.e-zest.com/generative-ai)
- [Healthcare](https://healthcare.e-zest.com)
- [Contact Us](https://www.e-zest.com/contact-us)

- ![Elevating Product Engineering with AI and ML](https://blog.e-zest.com/hubfs/Elevating%20Product%20Engineering%20with%20AI%20and%20ML.jpg)

  [BLOGS](https://blog.e-zest.com)
  
  #### Elevating Product Engineering with AI and ML

  As technology evolves and customer choices broaden, there is an increasing ...

  [Read more](https://blog.e-zest.com/elevating-product-engineering-with-ai-and-ml)
- ![e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024](https://blog.e-zest.com/hubfs/e-Zest%20%E2%80%93%20An%20Accion%20Labs%20Company%20-%20Gets%20Recognition%20as%20a%20Contender%20in%20the%20ISG%20Provider%20LensTM%20Study%20on%20Digital%20Engineering%20Services%202024.jpg)

  [NEWS](https://blog.e-zest.com/news)
  
  #### e-Zest – An Accion Labs Company gets Recognition as a Contender in the ISG Provider Lens™ Study on Digital Engineering Services 2024

  We are delighted to announce that e-Zest – An Accion Labs Company has been ...

  [Read more](https://blog.e-zest.com/news/e-zest-an-accion-labs-company-gets-recognition-as-a-contender-in-the-isg-provider-lens-study-on-digital-engineering-services-2024)
- ![e-Zest Solutions supports Digital Pune Mission](https://blog.e-zest.com/hubfs/blog-files/Digital%20Literacy%20Bus%20e-Zest.png)

  [CSR](https://blog.e-zest.com/csr)
  
  #### e-Zest Solutions supports Digital Pune Mission

  **Pune, India:** e-Zest Solutions, a leading digital technology innovation ...

  [Read more](https://blog.e-zest.com/csr/e-zest-solutions-supports-digital-pune-mission)

- [Work](https://www.e-zest.com/our-work)
- [Services](https://www.e-zest.com/our-services)
- [Insights](https://blog.e-zest.com)
- [Careers](https://www.e-zest.com/careers)
- [Enterprise Data](https://www.e-zest.com/enterprise-data-services)
- [Generative AI](https://www.e-zest.com/generative-ai)
- [Innovations](https://www.e-zest.com/innovations)
- [Healthcare](https://healthcare.e-zest.com)
- [About](https://www.e-zest.com/about-us)
- [Contact](https://www.e-zest.com/contact-us)

- [Blog](https://blog.e-zest.com/)
- [News](https://blog.e-zest.com/news)
- [CSR](https://blog.e-zest.com/csr)

- [![Linkedin Icon](https://blog.e-zest.com/hubfs/icons/linkedin_icon_white.svg)](https://www.linkedin.com/company/e-zest-solutions/)
- [![Twitter Icon](https://blog.e-zest.com/hubfs/twitter-logo-silhouette%20(5).svg)](https://twitter.com/ezest)
- [![Facebook Icon](https://blog.e-zest.com/hubfs/facebook-app-logo%20(4).svg)](https://www.facebook.com/61259256638)
- [![Instagram Icon](https://blog.e-zest.com/hubfs/instagram-logo%20(4).svg)](https://www.instagram.com/ezestsolutions/)
- [![Youtube Icon](https://blog.e-zest.com/hubfs/icons/youtube_icon_white.svg)](https://www.youtube.com/user/ezestcorporate)

[![Go to top icon](https://blog.e-zest.com/hubfs/AA%20Assets/top%20button.svg)](https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol#top)

Hit enter to search or ESC to close

Insights

e-Zest members share technology ideas to foster digital transformation.

- [Blog](https://blog.e-zest.com)
- [Whitepaper](https://blog.e-zest.com/whitepaper)
- [Playbook](https://blog.e-zest.com/playbook)
- [News](https://blog.e-zest.com/news)
- [Innovation](https://blog.e-zest.com/innovation)
- [Media Kit](https://www.e-zest.com/media-kit)

# Tracking Magento 2 backend orders in Google Analytics using Measurement Protocol

[Sarang Bhantar](https://blog.e-zest.com/author/sarang-bhantar)  Sep 29, 2018

[Magento 2.0](https://blog.e-zest.com/topic/magento-2-0) [eCommerce](https://blog.e-zest.com/topic/ecommerce) [Google Analytics](https://blog.e-zest.com/topic/google-analytics)

- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol&title=&summary=&source=>
- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol&text=Tracking%20Magento%202%20backend%20orders%20in%20Google%20Analytics%20using%20Measurement%20Protocol>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol>
- <https://www.instagram.com/ezestsolutions/?u=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol>

e-Commerce tracking is of immense importance and a significant number of merchants use Google Analytics for tracking all the transactions or order details. 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. This problem further leads to a situation where reports from Google Analytics won't match the reports generated from Magento. Imagine if your financial reports are missing a bulk of the orders.

To overcome this issue we can make use of the Measurement Protocol provided by Google and implement it into Magento 2. Before we begin please read and get an overview of the Measurement Protocol that will be useful to understand and customize if your requirement is different from what has been explained in this blog.

Create a custom module in Magento 2 or make use of an already created module, the choice is yours. Once you have the basic skeleton of your module we can move ahead.

**Step 1: Add an Observer**

We will make use of an observer that will catch the event when an order is being created in Magento Admin. For implementing this define the oberver event and the class that will take action on that event in the events.xml file which should be located in the etc/adminhtml folder in your module, the path will be like MyPackage/MyModule/etc/adminhtml/events.xml and the code can be like below:

*\<?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="sales\_model\_service\_quote\_submit\_success"\>*  
*\<observer name="mymodule\_analytics\_order\_success" instance="MyPackage\\MyModule\\Observer\\AdminOrderPlaced"/\>*  
*\</event\>*  
*\</config\>*

We have placed the events.xml inside adminhtml directory because we want to catch the event only when the order is placed in the adminhtml area of Magento.

Why use the event "sales\_model\_service\_quote\_submit\_success" ? The reason to use this particular event is because at this point in the order creation process the order is actually saved in the database and the entity id for the order is generated. So at this point we will be assured that the order is successfully placed in Magento and is ready to be sent in Google Analytics. You can modify the event according to your needs.

Create the class that will take action on the event we want to listen. In this case create file AdminOrderPlaced.php under MyPackage\\MyModule\\Observer, and put the below code in this file.

\<?php  
namespace MyPackage\\MyModule\\Observer;  
use Magento\\Framework\\Event\\Observer as EventObserver;  
use Magento\\Framework\\Event\\ObserverInterface;  
class AdminOrderPlaced implements ObserverInterface  
{  
 /\*\*  
 \* @var \\Magento\\Sales\\Model\\Order  
 \*/  
 protected $\_order;  
 /\*\*  
 \* @var \\MyPackage\\MyModule\\Helper\\Data  
 \*/  
 protected $\_helper;  
/\*\*  
 \* @var \\Magento\\Framework\\App\\State  
 \*/  
 protected $\_state;  
 /\*\*  
 \* @param \\MyPackage\\MyModule\\Helper\\Data $data  
 \* @param \\Magento\\Framework\\App\\State $state  
 \*/  
 public function \_\_construct(  
 \\MyPackage\\MyModule\\Helper\\Data $data,  
 \\Magento\\Framework\\App\\State $state  
 )  
 {  
 $this-\>\_helper = $data;  
 $this-\>\_state = $state;  
 }  
 public function execute(EventObserver $observer)  
 {  
 $this-\>\_order = $observer-\>getOrder();  
 $this-\>\_helper-\>sendOrderToGoogle($this-\>\_order);  
 }  
}

In the above code we have injected a helper class which we will create shortly and we will define a function in this class that will implement the Measuremet Protocol API, helper class will be useful in case your application is creating orders from multiple code sections like programmatic approach so just by calling the functions defined in this helper class will make your life easy. The helper function will accept an object of Sales/Order so we don't have to reload the order object again in this case.

**Step 2: Saving Google Analytics Tracking Id in Magento 2**

Add the Google Analytics Tracking Id (Example: UA-XXXXXXX-X) into Magento configuration. You can find the tracking id from your analytics account, navigate to Admin Settings -\> Property Settings. To save this id in Magento 2 navigate to Stores -\> Configuration -\> Sales -\> Google API -\> Google Analytics. In this configuration select 'Universal Analytics' under Account Type, enter the tracking id into the Account Number field and save the configuration.

**Add the Helper Class**

Now lets create a helper function which will implement the API used to send the order information to Google. Create a new file Data.php under directory MyPackage\\MyModule\\Helper containg the below code

\<?php  
namespace MyPackage\\MyModule\\Helper;  
use \\Magento\\Framework\\App\\Helper\\AbstractHelper;  
class Data extends AbstractHelper  
{  
 const GA\_PROPERTY\_ID = "google/analytics/account";

/\*\*  
 \* @var \\Magento\\Store\\Model\\StoreManagerInterface  
 \*/  
 protected $\_storeManager;  
 /\*\*  
 \* @var \\Magento\\Framework\\App\\Config\\ScopeConfigInterface  
 \*/  
 protected $\_scopeConfig;

/\*\*  
 \* @var \\Magento\\Framework\\HTTP\\Client\\Curl  
 \*/  
 protected $\_curl;

 /\*\*  
 \* @var \\Magento\\Framework\\HTTP\\Header  
 \*/  
 protected $\_http;

 /\*\*  
 \* @param \\Magento\\Store\\Model\\StoreManagerInterface $storeManager  
 \* @param \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig  
 \* @param \\Magento\\Framework\\HTTP\\Client\\Curl $curl  
 \* @param \\Magento\\Framework\\HTTP\\Header $http  
 \*/  
 public function \_\_construct(  
 \\Magento\\Store\\Model\\StoreManagerInterface $storeManager,  
 \\Magento\\Framework\\App\\Config\\ScopeConfigInterface $scopeConfig,  
 \\Magento\\Framework\\HTTP\\Client\\Curl $curl,  
 \\Magento\\Framework\\HTTP\\Header $http  
 )  
 {  
 $this-\>\_storeManager = $storeManager;  
 $this-\>\_scopeConfig = $scopeConfig;  
 $this-\>\_curl = $curl;  
 $this-\>\_http = $http;  
 }  
 public function sendOrderToGoogle(\\Magento\\Sales\\Model\\Order $order)  
 {  
 $data = \[\];  
 $url = 'https://www.google-analytics.com/collect';  
 $debugUrl = "https://www.google-analytics.com/debug/collect"; // Change url for testing the API

 if($order-\>getId()) {  
 $store = $this-\>\_storeManager-\>getStore($order-\>getStoreId());

$gaId = $this-\>\_scopeConfig-\>getValue(self::GA\_PROPERTY\_ID,  
 \\Magento\\Store\\Model\\ScopeInterface::SCOPE\_STORE,  
 $store);

 $affiliation = 'admin'; // To recognize that order is placed in admin  
 if($gaId) {  
 $data\['v'\] = 1;  
 $data\['tid'\] = $gaId;  
 $data\['cid'\] = $order-\>getCustomerId();  
 $data\['t'\] = 'transaction';  
 $data\['ta'\] = $affiliation;  
 $data\['ti'\] = $order-\>getIncrementId();  
 $data\['tr'\] = $order-\>getGrandTotal();  
 $data\['ts'\] = $order-\>getShippingAmount();  
 $data\['tt'\] = $order-\>getTaxAmount();  
 $data\['cu'\] = 'USD';

 if($this-\>sendData($data,$url)) {  
 // If order is sent successfully send order items  
 $data = \[\];  
 $data\['v'\] = 1;  
 $data\['tid'\] = $gaId;  
 $data\['cid'\] = $order-\>getCustomerId();  
 $data\['t'\] = 'item';  
 $data\['ti'\] = $order-\>getIncrementId();  
 foreach($order-\>getAllVisibleItems() as $item) {  
 $data\['in'\] = $item-\>getName();  
 $data\['ip'\] = $item-\>getPrice();  
 $data\['iq'\] = $item-\>getQtyOrdered();  
 $data\['ic'\] = $item-\>getSku();  
 $data\['cu'\] = 'USD';  
 $this-\>sendData($data,$url);  
 }  
 }  
 }  
 }  
 }   
 private function sendData($data,$url)  
 {  
 $url = $url."?".http\_build\_query($data);  
 $userAgent = $this-\>\_http-\>getHttpUserAgent();  
 if(!$userAgent) { // User agent is a required option  
 $userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311  
Firefox/2.0.0.13';  
 }

$curlOptions = array(CURLOPT\_SSL\_VERIFYPEER =\> 0, CURLOPT\_TIMEOUT =\> 10,  
CURLOPT\_USERAGENT =\> $userAgent);  
 try {  
 $this-\>\_curl-\>setOptions($curlOptions);  
 $this-\>\_curl-\>get($url);  
 $response = $this-\>\_curl-\>getBody();  
 if($this-\>\_curl-\>getStatus() != 200)  
 return false;  
 } catch(Exception $e) {  
 return false;  
 }  
 return true;  
 }  
}

Notice the function sendOrderToGoogle where we are making a curl request to the API url and sending the necessary order details. The user agent has to be set so make sure that it is sent in the request. You can debug your implementation by changing the API url to a debug url commented in the code. Go through the documentation of validating hits [here](https://developers.google.com/analytics/devguides/collection/protocol/v1/validating-hits).

**Important Note:** The transactions will be reflected in Google Analytics within 48 hours, so do not expect to see your orders instantly in analytics.

Similarly you can implement other data tracking requirements in Magento 2.

- <https://twitter.com/intent/tweet?source=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol&text=Tracking%20Magento%202%20backend%20orders%20in%20Google%20Analytics%20using%20Measurement%20Protocol>
- <https://www.facebook.com/sharer/sharer.php?u=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol>
- <https://www.linkedin.com/shareArticle?mini=true&url=https://blog.e-zest.com/tracking-magento-2-backend-orders-in-google-analytics-using-measurement-protocol&title=&summary=&source=>

### Similar Blog

[![How to setup Docker Container for Magento 2](https://blog.e-zest.com/hubfs/How%20to%20setup%20Docker%20Container%20for%20Magento%202.jpg) How to setup Docker Container for Magento 2 Docker is an application that allows us to manage multiple applications in the simplest way in the docker container. Docker ... By Rajesh Rathod Oct 15, 2021](https://blog.e-zest.com/how-to-setup-docker-container-for-magento2)

[![Magento 2: Module Code Generation with Pestle](https://blog.e-zest.com/hubfs/blog-files/Magento%202%20%20Module%20code%20generation%20with%20Pestle.png) Magento 2: Module Code Generation with Pestle **What is Pestle?**The Pestle is a new PHP (5.6+) framework for creating and organizing PHP programs into Python like libraries of ... By Makarand Uparwat Nov 20, 2018](https://blog.e-zest.com/magento-2-module-code-generation-with-pestle)

[Custom Sessions in Magento 2 Sessions play an important role in enhancing the user experience and they also help gather vital stats about the user visits. In ... By Pooja Pardeshi Nov 15, 2018](https://blog.e-zest.com/custom-sessions-in-magento-2)

[![Magento 2 - How to display CMS block in Checkout Sidebar](https://blog.e-zest.com/hubfs/how-to-display-cms-block-in-checkout-sidebar.png) Magento 2 - How to display CMS block in Checkout Sidebar Using CMS blocks, site administrator can easily manipulate content of the vendor. CMS blocks can be used to display promotional ... By Sejal Shah Nov 01, 2018](https://blog.e-zest.com/magento-2-how-to-display-cms-block-in-checkout-sidebar)

[![Code validations using static-review library in Magento](https://blog.e-zest.com/hubfs/code-validations-using-static-review-library-in-magento.png) Code validations using static-review library in Magento **Introduction:**Magento2 comes up with some advance tools and plugins, which are really useful for developers to track some common ... By Shubh Sharma Oct 19, 2018](https://blog.e-zest.com/code-validations-using-static-review-library-in-magento)

[![Maintaining Code in GIT](https://blog.e-zest.com/hubfs/blog-files/maintaining-code%20in-git.png) Maintaining Code in GIT GIT has been the primary choice of version control system for a long time, and it should be, because of how the GIT system is ... By Sarang Bhantar Oct 19, 2018](https://blog.e-zest.com/maintaining-code-in-git)

[![Delivering Omni-Channel Customer Experience with Magento](https://blog.e-zest.com/hubfs/Delivering%20Omni-Channel%20Customer%20Experience%20with%20Magento.png) Delivering Omni-Channel Customer Experience with Magento Organizations aspiring to either step into the world of omni-channel or transform their existing multi-channel into unified ... By Sarang Bhantar Sep 06, 2018](https://blog.e-zest.com/delivering-omni-channel-customer-experience-with-magento)

[![Omni-Channel vs Multi-Channel](https://blog.e-zest.com/hubfs/e-Zest%20Multichannel%20Vs%20Omni-channel.jpg) Omni-Channel vs Multi-Channel When it comes to the world of commerce, omni-channel and multi-channel are the reigning buzzwords. In other words, while ... By Sarang Bhantar Sep 03, 2018](https://blog.e-zest.com/omni-channel-vs-multi-channel)

## Helping Organizations Thrive in the Digital Age

[Contact Us](https://www.e-zest.com/contact-us)

- [About](https://www.e-zest.com/about-us)
- [Services](https://www.e-zest.com/our-services)
- [Work](https://www.e-zest.com/our-work)
- [Insights](https://blog.e-zest.com/)
- [Healthcare](https://healthcare.e-zest.com/)
- [Accion Labs](https://www.accionlabs.com/)

- <https://www.linkedin.com/company/e-zest-solutions/>
- <https://twitter.com/ezest>
- <https://www.facebook.com/eZest/>
- <https://www.instagram.com/ezestsolutions/>
- <https://www.youtube.com/user/ezestcorporate>

- Dallas
- Detroit
- Hannover
- Vienna
- London
- Pune

 © 2025 e-Zest Solutions

- [Privacy Policy](https://www.e-zest.com/privacypolicy)
- [Cookie Policy](https://www.e-zest.com/cookie-policy)

 Contact us

 Contact us

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.