Firing Offline transaction to Google Analytics via the Measurement Protocol
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 – 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
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:
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
userID = row
transactionID = row
affiliate = row
revenue = float(row.replace(‘$’,”))
tax = float(row.replace(‘$’,”))
shipping = float(row.replace(‘$’,”))
coupon = row
prodID = row
prodName = row
prodCategory = row
prodBrand = row
prodVariant = row
prodPosition = row
prodQuantity = row
transaction = transactionID+’&pa=purchase&ti=’+transaction+’&pr1qt=’+prodQuantity+’&tr=’
product = ‘&pr1id=’+prodID+’&pr1nm=’+prodName+’&pr1ca=’+prodCategory+’&pr1br=’+prodBrand
values = ‘v=1&t=pageview&tid=’+property+’&cid=’+userCookieID+’&uid=’+userID
requests.post(url, data =values)
This is the way your transaction csv file suppose to look like:
With the following columns:
userCookieID, userID, transactionID, affiliate, revenue, tax, shipping, coupon, prodID, prodName, prodCategory, prodBrand, prodVariant, prodPosition, prodQuantity