Upcoming LunaMetrics Seminars
New York City, Aug 4-8 San Francisco, Aug 11-15 Los Angeles - Anaheim, Sep 8-12 Washington DC, Sep 22-26

Using APIs To Add Insight To Google Analytics


Want to segment your users by whether they live in a cool neighborhood in GA? We’ve got an API for that.

How Much Does Scrap Metal Cost?

About a year ago we were working with a client who wanted to bring some very specific data into their Google Analytics account. They wanted to compare their site traffic with the current price of scrap metal, by adding it as a Custom Variable on the visit. While scrap metal prices aren’t exactly something that would help many other clients, we started thinking more about different kinds of ways we could pull other information from APIs around the web, into Google Analytics, in ways that would increase our insight.

Wait Back Up. What’s an API?


For the non-nerds amongst us (Welcome! We won’t hurt you. One of us. One of us.) API stands for Application Programming Interface. It’s basically a set of rules that allows different programs to talk to each other. For instance, we can use them to ask a question, from our website, to another web location, and the API there will give us an answer we can use. Sometimes it’s just basic information like:

My client’s website: “Hey Scrap Metal API! What’s the current price of Scrap Metal?”

Scrap Metal API: “$100 a pound.”

Other times it’s more involved, like embedding maps or videos onto a page.

There are thousands of APIs. Google has a ton. Social Media like Facebook and Twitter have APIs that let you access information on likes, or if the user is logged in, even some of that user’s information (if they’ve given you permission). There’s a ton of information out there.

What’s the conversion rate for people north of the 38th Parallel?


Google has a Geocoding API  which lets you convert any address into geographic coordinates. Maybe you are capturing someone’s address in a form; you could translate that to coordinates, and throw that into Custom Dimensions in Google Analytics. It’s no longer personally identifiable information, so you could insert latitude and longitude points into your data, and then later export them and map them using other APIs.  Let’s compare people within a certain distance of the demilitarized zone and see what their conversion is like. (Hint, they probably don’t have electricity so you won’t see many conversions…)

Addendum: Jon Meck, here at LunaMetrics is a coward about pushing the PII boundaries and thinks this is a bad idea. I say he’s wrong, I don’t see latitude and longitude as PII.  What do you think? Is it PII? Let us know your thoughts in the comments. If it bothers you though and you worry that it’s PII just round it to the nearest degree so it’s less accurate.

What’s my social media engagement like from neighborhoods containing “Makin’ It Singles”?


Zillow has a neat API called the GetDemographics API  which lets you send them an address or zip-code and get back a bunch of interesting information about that location. What’s the median home value? What’s the age of the homes? Is it a neighborhood with more than 50% homes built since 2000? Does it contain the “Makin’ It Singles” demographic group?

Rather than adding a person’s address, you could simply immediately import some of this information into Google Analytics. Capture the information from the user on a form, query the API on the results page using JSON, and then insert it into a Custom Dimension for the user.

Zillow Neighborhood Home Value = 220,600
Zillow Average Year Built = 1980
Zillow Demographic Segments = “Makin’ It Singles|Aspiring Urbanites|Bright Lights, Big City”

It’s not necessarily about that user, but it’s certainly more interesting than segmentation based on city or region/state. Why look at metros when I can segment by people who live in the types of neighborhoods with a transportation characteristic of “Commutes By Bicycle”?

How can Weatherlytics optimize my ad spend?

Is someone more likely to order an umbrella online if it’s raining, even if it’s going to take 2 days to be delivered? Why not see what the user’s weather is currently like with a weather API like the one from World Weather Online? This one is a personal favorite of mine, and one of the earlier ones we hooked up. We can capture the weather, temperature, barometric pressure, wind, and more from local conditions of our visitors. Here’s how it shows up in our old classic analytics account (which has data for a longer time span than our newer universal account, but you could obviously put this data into a Custom Dimension just as easy as a Custom Variable). For about a year we’ve been collecting weather data, and finally I wanted to see if there was any statistical significance in any of it.

apiblog11 apiblog12


Left: Data with temperature(F), humidity, barometric pressure, and wind speed. Right: The weather code passed into Google Analytics

I pulled out some data and crunched temperatures and general descriptions. Mostly weather didn’t seem to have a statistically significant effect on registrations for our Google Analytics, Adwords and Tag Manager trainings, except in one area. Temperature. The hotter it gets, it looks like the less likely people are to sign up for our seminars. Anything over 70 degrees seems to affect conversions for the worse.


Of course we had less traffic because there were fewer days of those temperatures, but it remained statistically significant. Why? I dunno. Maybe people are more likely to be indoors and looking for training when it’s colder, and more likely to be outside enjoying the day when it’s warmer.

So what can we do because of this? Maybe we could lower our ad spend when we know it’s over 90 degrees somewhere because people are just not interested in registering for training at those times. If it’s 50 degrees? Up that ad spend my friend. People will be primed to buy the colder it is.

What’s the big takeaway?

Don’t be limited to the information Google Analytics provides, because you don’t have to be. Is there something you’d like to know about a user, or their neighborhood, or their current local weather conditions, or the price of gold, or something else? Maybe there is an API out there that can provide you that information. Get that information from the API, and put it into Google Analytics as a Custom Dimension and start figuring out if those “Makin’ It Singles” neighborhoods with an average home value of over $200,000 are more or less likely to buy an umbrella online if it’s rainy outside, and do it all inside Google Analytics.

What APIs are YOU using to bring additional insights into Google Analytics?

Are there any you’d be interested in us providing some specific code and solutions for?

Comment below and let us know!


Sayf Sharif

About Sayf Sharif

Sayf Sharif is a Web Analyst, and expert in Usability and UX, who has worked with businesses large and small to maximize their online presence since the beginning of the Web, winning numerous awards along the way. Sayf has studied human tool use from the stone age (he went to graduate school for Archaeology) to the information age (he started programing on his father’s TRS-80), and is always interested in what goals people wish to accomplish using their tools, and how successful that experience was.


11 Responses to “Using APIs To Add Insight To Google Analytics”

Andrew Garberson Andrew Garberson says:

This is such a fun conversation and the e-commerce opportunities are nearly endless. Lunar phases affect the way some people eat, sleep, act. Looks like there’s an API, too.

Adam says:

Thanks for the article, interesting read :)

Sayf Sharif Sayf Sharif says:

That’s a great one Andrew. I should have mentioned it in the post, but a good resource for API’s is http://www.programmableweb.com/ where you can find all sorts of stuff.

Another one you can find on the above link is MyGasFeed API which lets you get current local gas prices. If your business involves the price of gas, that could be a good hit level custom dimension to see if gas prices affect things. If gas prices are high, are the visits to your travel/vacation home rental website down? etc.

We pull in school vacations, as well as weather for some clients. Love the Demographics API! i’ll have to look for something similar in Holland.

About location being a PII, it’s not accurate enough to be a PII, but the question ‘is it PII’? arises in our office a lot to. For some clients we save postal codes in GA, but only the first 4 numbers. That’s neighbourhood level. If we would save the whole postal code you would know the location on street level, which could be PII (depending on street length ;) )

Sayf Sharif Sayf Sharif says:

Vacations are a good one.

As far as PII it gets into this weird grey area. with an accurate latitude and longitude you can pinpoint a house, but who is it in that house that your’e talking about?

Also it’s two pieces of information. Latitude and Longitude. Each one independently isn’t going to identify anyone, only when used together will they narrow it down. What’s the difference then between those two pieces of information and three pieces of information to identify someone? There was a study here in the states where the postal code, gender, and birthdate of someone could identify the person within 95% accuracy. that’s arguably more accurate than with the two points of latitude and longitude, yet those three pieces of information have been confirmed as not being PII by Google.

I fall on the side of the fence in this argument that what PII is in the case of GA is HARD PII, that is very explicit information. Your social security # in the states, or other government issued id. Your credit card number. Etc. Information that you shouldn’t store, single pieces of information that are very explicitly PII.

Until someone tells me otherwise, I”m good with Latitude and Longitude. :P

Hazel says:

Hi Sayf,
Do you have any articles on how to use the world weather online API to plug data into a GA custom variable?
Many thanks,

Pietro says:

May I point you to MoreMetrics.net? We are trying to offer a tool to do exactly that, i.e. import more metrics into GA. Hope it helps.

Sayf Sharif Sayf Sharif says:


We don’t have specific instructions for it, we may do it in a future blog. It’s relatively easy if you have some basic programming skills. We take the user’s IP address, and then pass that to the API, which returns JSON. We then parse that response, and create custom dimensions, variables, events, etc based on it.

Sayf Sharif Sayf Sharif says:

Thanks to Google Analytics for linking to this post in their email newsletter.

One tweet I saw mentioned that this could lead to spurious correlations, and they’re totally right. I’d just like to add this:


No matter what data you add, always be cautious for spurious correlations.

Aurelie Pols says:

Cool stuff!

To answer to your question about what is PII and what it’s not, see slides 17 & 18 of http://www.slideshare.net/MindYourPrivacy/the-future-of-privacy-aurlie-pols-at-ibm-smarter-commerce-global-summit-2014.
There’s your list, according to US state legislation, as our legal team analysed it at the end of last year. You’ll also find the exceptions per state as not each state has the same list of variables…

Having said the Terms of Use of GA changed their policy, replacing the words PII by the more European approach of “uniquely identifying an individual” as we wrote about a year ago: http://www.mindyourprivacy.com/blog/english-us-google-analyticss-privacy-policy-looses-2-little-words-what-does-this-mean-to-the-digital-analytics-industry/

And as you mentioned, by mixing 3 variables such as zip code, gender and data of birth, a study by Microsoft back in 2000 found that you could identify 87% of the US population. This study can be found here: http://impcenter.org/wp-content/uploads/2013/09/Simple-Demographics-Often-Identify-People-Uniquely.pdf

Now, indeed, this is still a grey zone but I would like to share a word of caution: this is a matter of liability for your company.
If something goes wrong somewhere (I’ll let you decide on the probabilities based on the access, sensitivity of the sector your client is in and region to name just a few things to consider), you can be sure that Google will not cover for you. So if you are getting close to the grey zone and they can prove in court that you are liable, guess who wins and doesn’t go bankrupted?

Last but not least, certainly for the Europeans watching this post, another word of caution. Google Analytics free version considers itself both a processor as a controller. You might want to ponder on that one for some of your clients, depending upon the data your collecting and merging together. We wrote a little fable about that one, just for fun: http://www.mindyourprivacy.com/blog/english-us-role-playing-which-one-are-you-google-analytics-controller-or-processor/.

I hope this helps. I’m not saying don’t do all the funky data stuff, on the contrary. Just make sure you cover your ass. Better be safe than sorry, happy API adding,
Kind regards,

Sayf Sharif Sayf Sharif says:

Great comment, Aurélie, and thanks for the links.

When people generally ask me whether they can track something I pull the old “Consult Your Attorney” canard that I learned from Google. Their TOS is mostly there to protect THEM, and as you mention in no way does it protect YOU.

Leave a Reply