In this post, I will show you how to streamline your eCommerce tracking implementation and upload product data to Google Analytics.
From a remote CSV file.
Over Google Sheets.
Automatically.

Sounds good? Here we go!

TL;DR: I have a Github project to help you upload product data for Google Analytics Enhanced eCommerce via the API

Your Google Analytics eCommerce tracking is a temple

So you implemented Google Analytics Enhanced eCommerce tracking on your site? You charged up on product attributes? Everything looked great but you’re missing lots of data and/or transactions? I get this a lot from clients because their setup is far from perfect…

Many clients of mine saw GA’s EEC (Enhanced eCommerce) as a potential data smorgasbord, in which every single product attribute should be filled: tons of custom dimensions, custom metrics, extensive product categories – the works! Unfortunately, overloading your EEC tracking code with so much data tends to break tracking requests.
It is time to take better care of your EEC implementation!

GET, POST and FAIL

The main culprit when it comes to GA EEC tracking failure is when the size of the tracking request exceeds the maximum allowed size.

If we consider how Google Analytics’ Measurement Protocol works, with all of its parameters, hits can fail if the requests get too big.

When the request size exceeds 8192 bytes (characters), the tracking request fails and traffic data does not get sent to Google Analytics servers.

How or why would you ever exceed that limitation?

So glad you asked!

Let’s assume you’re tracking a shopping cart containing 10 items. Each item contains a whole slew of attributes:

  • Product ID (SKU) and name
  • Category (w/ hierarchy)
  • Brand
  • Custom dimensions
  • Custom metrics
  • Product variant
  • Product price
  • And more!

Now imagine all the attributes are populated with sizeable values and labels and you can quickly reach 8192 characters, not including all the boilerplate Measurement Protocol parameters.

What can I do to fix it?

The first thing you can do is streamline tracking requests and only send the data you really need.

Stick to basics: only supply a product ID (SKU), quantity and unit price.

But what about all my attributes?

– You, shocked by such a minimalist approach

Fear not, we can use Google Analytics’ Data Import functionality to send those attributes! The goal here is to build a text file (comma-separated values, aka CSV) containing a common key (product ID/SKU) and related attributes. For instance:

IDNameCategory
1234Awesome WidgetWidgets
2345Super WidgetWidgets
3456Ultimate Widget Widgets

Once we have a CSV set up (manually or generated via PHP/Python/R/etc.) we can create a Data Import schema in Google Analytics.

Go to your administration panel, go to your property then go to Data Import.

Select Product Data
Name the import and select views where data will be processed
Map columns
Define overwrite settings

Mapping your columns provides you with a CSV schema with column headers you will need to follow.

Once you go through the steps, you’re all set! Now you can manually upload product data thanks to your CSV file.

Your Google Analytics Enhanced eCommerce reports now contain all the attributes contained in your CSV upload.

Every time Google Analytics sees a product ID, it will look in the import data and map provided values for product fields matching the product ID/SKU key. Amazing, right?

How often do I need to upload product data?

It boils down to how often new products appear/disappear in your catalog. If you add/remove products from your catalog on a daily basis then you need to upload your CSV on a daily basis. Unfortunately, unless you have strong publishing processes in place, you will tend to forget to upload said CSV and your reports will not contain the right product attributes 😐

Which is why I am offering a method to automatically upload your CSV file ad upload product data to Google Analytics. Of course you could use the Google Analytics Management API and code something with Python, Java or the language of your choice. Then you’d need to add scheduling in the form of a cron job.

With my method, I’m going to show you how to do it all in Google Sheets!

Create a blank Google Sheet

  1. Visit my Github repository
  2. Go to Tools > Script Editor
  3. Edit the Code.gs file and use/adjust the code provided in this project to match your Google Analytics account / import settings

In the editor, go to Resources > Advanced Google services

In the list of services, scroll down to Google Analytics API and enable it

Testing your script

To test your script, simply select the uploadProductFeed function and click the “Play” button to the left of the drop-down menu. Go through the Google authentication dialog (you may need to use 2-factor authentication)

You also go to View > Execution transcript to look at console output

Go to your Google Analytics property and look for new uploads sent by the script via the API

Extra credit: scheduling uploads

Assuming your product feed is updated regularly, you can schedule your script to run on a regular basis. Thankfully, Google Sheets provides this scheduling for you.

  • From the script editor, click the “clock” icon or go to Edit > Current project’s triggers
  • If the trigger list is empty, click the blue button to create a new trigger
  • In the function dropdown menu, select your uploadProductFeed function
  • In event source, select Time-driven then select a frequency, e.g. Week timer and set it to run (for instance) every Monday at 8am
  • Save your trigger and enjoy your automatically updated product attributes in Google Analytics!

In closing

When it comes to working with data, it’s good to be lazy, erm, I mean to have efficient tools 😅

The method I describe in this post minimizes the risk if tracking hits failing because of data overload and ensures that I can upload product data via the Google Analytics API.

What about you? Have you set up data imports in Google Analytics? What went well? What went wrong? Let me know in the comments!