Firing Offline transaction to Google Analytics via the Measurement Protocol

Offline Transaction

Google Analytics enables us to track almost everything that happens on the client side (online data):
Every page that was viewed, every click on any banner, picture, link, icon, button etc. We also can track how much the user scrolled, for how long he stays, what product he viewed, added to cart and I can go on and on.
There’s one thing that you can’t send from the client side to GA, simply the events that don’t happen on the client side.
For example an offline transaction:
A potential customer of yours searches for your product in Google, clicks on your ad, views your site, sees a coupon and downloads it, he even finds the closest store to where he lives, so far, all can be tracked in GA.
The next day, he appears in your store, buys the product (in an offline transaction) and uses the coupon. This you would like to send to GA, complete the whole journey of your customer, give the right credit to the ad that drove the customer to download the coupon and then purchase. What about the AB test you may be running? this will defiantly help you determine the better variant.

This article will guide you how to fire the transaction to GA. I will provide you the code I built in Python that you can make your small adjustments for your business and fire your transaction automatically.

Assuming you are using enhanced eCommerce, if not I really recommend you transforming to the new eCommerce reports GA provides, you can read in this article some of the advantages. The second very important feature is the userID, enables us to link two different sessions from different devices to the same user.
We won’t be able to associate the impression to the transaction, this may be done only in the same session, but we will be able to send the coupon, the user and associate the traffic source of our user.

This is the hit you need to fire to GA:

v=1&t=pageview&tid=UA-1111111-1&cid=555&dh=OptimizingAudience.com&dp=%2Freceipt&dt=Receipt%20Page&ti=T12345&ta=OptimizingAudience&tr=37.39&tt=2.85&ts=5.34&tcc=summerCoupon&pa=purchase&pr1id=P12345&pr1nm=productName&pr1ca=productCategory&pr1br=productBrand&pr1va=productVariant&pr1ps=productPosition&pr1qt=productQuantity

v – This is the version, needs to be 1
t – The hit type, may be pageview, event, transaction
tid – Your property ID suppose to look lie this: UA-123456789-1
cid – UserID google anlytics uses, this is actually the CookieID, you can find it in your user explorer report
User Explorer
dh – Your domain (host name), for example: OptimizingAudience.com
dp – The page that “fired” the hit, you can indicate here that this isn’t your regular thank-you page
ti – This is the offline transaction ID of the transaction.
ta – The affiliate, in case you had an affiliate for this transaction you can declare the name of the affiliate here. In case you don’t have one, you can insert the name of your brand.
tr – Transaction revenue, this is the sale amount (before tax and shipping)
tt – Transaction tax, the amount of tax on this transaction
ts – Transaction shipping.
tcc – Transaction coupon, here you can enter the coupon code (in case the customer used one)
pa – Should be “purchase” indicates this hit is a purchase
pr1id – The product ID your customer bought, in case he bought another product in the same transaction you can add another parameter with the number 2: pr2id
pr1nm – The name of the product your customer bought, in case he bought another product in the same transaction you can add another parameter with the number 2: pr2nm
pr1ca – The product category (in case your product has one)
pr1br – Product Brand
pr1va – This is a variant you can add to the product, for example it’s size, color, type, wight etc.
pr1ps – The position of the product, in case you want to attribute the product position to the sale (you will probably save the position of the product when generating the coupon code)
pr1qt – The quantity of products form the same kind in the same transaction. Foe example, in case your customer bought 1 phone and 3 same earphones you will fire: pr1qt=1&pr2qt=3

after setting all of these parameters properly, all you need is to configure the following code (written in Python 2) and start receiving your offline transaction in your Google Analytics:

import requests
import csv
property = ‘UA-12345678-1’ # replace with your property here
hostname = ‘OptimizingAudience.com’ # replace with your hostname here
pagePath = ‘receiptPage’ # replace here with your thank-you page you want to present
pagheTitle = ‘receipt’ # replace here with your thank-you page title you want to present
url = ‘https://www.google-analytics.com/collect’
with open(‘transactions.csv’, ‘rb’) as csvfile:
  spamreader = csv.reader(csvfile, delimiter=’,’, quotechar=’|’)
  for row in spamreader:
   userCookieID = row[0]
   userID = row[1]
   transactionID = row[2]
   affiliate = row[3]
   revenue = float(row[4].replace(‘$’,”))
   tax = float(row[5].replace(‘$’,”))
   shipping = float(row[6].replace(‘$’,”))
   coupon = row[7]
   prodID = row[8]
   prodName = row[9]
   prodCategory = row[10]
   prodBrand = row[11]
   prodVariant = row[12]
   prodPosition = row[13]
   prodQuantity = row[14]
   transaction = transactionID+’&pa=purchase&ti=’+transaction+’&pr1qt=’+prodQuantity+’&tr=’
+str(revenue)+’&tt=’+str(tax)+’&ts=’+str(shipping)+’&ta=’+affiliate+’&tcc=’+coupon
   product = ‘&pr1id=’+prodID+’&pr1nm=’+prodName+’&pr1ca=’+prodCategory+’&pr1br=’+prodBrand
+’&pr1va=’+prodVariant+’&pr1ps=’+prodPosition+’&pr1qt=’+prodQuantity
   values = ‘v=1&t=pageview&tid=’+property+’&cid=’+userCookieID+’&uid=’+userID
+’&dh=’+hostname+’&dp=’+pagePath+’&dt=’+pagheTitle+transaction+product
   requests.post(url, data =values)

This is the way your transaction csv file suppose to look like:
The csv file for transactions

With the following columns:
userCookieID, userID, transactionID, affiliate, revenue, tax, shipping, coupon, prodID, prodName, prodCategory, prodBrand, prodVariant, prodPosition, prodQuantity

Learn Google Analytics, at your own pace, from wherever you are, with the best platform!

Start the course now (free)