Using Universal Analytics to Measure Interaction with Assets that Live on Different Sites

This is a technical post that requires an understanding of JavaScript or at least a technical understanding of the web.

The new version of Google Analytics, Universal Analytics, has a little gem in it called the Measurement Protocol. The Measurement Protocol lets you send data to your GA account using HTTP POST requests. In other words, you can send data to GA without having to interact with your site. This is great when connecting online and offline activities. Read all about the Measurement Protocol here.

I love the mainstream data craze going on, especially quantified self, automated homes, google now, predictive modeling/machine learning and the internet of things. I approach every analytics issue with the mindset of how, not if. I believe everything can be tracked and measured you just have to figure out how.

The Idea

A month ago, I had this idea of creating an interactive asset but I wanted to be able to measure how the asset performs on different sites. For example, do people interact with it more on Mashable than on Techcrunch?  This used to need some custom analytics stuff and some heavy coding. But now with the Measurement Protocol you can push data straight to GA using JavaScript. Note: The biggest assumption is that the site/CMS must be able to execute the JavaScript that goes along with your asset.

How, you ask? Using the XMLHttpRequest API for JavaScript. For all the details about the API check out the XMLHttpRequest wikipedia page and for the technical documentation here is the MDN page on it.

Basically, the XMLHttpRequest makes server requests without having to reload the page and it plays a major role in AJAX. In this case, it fires off the Measurement Protocol every time an action occurs that you want to track.

In this example, I am attaching XMLHttpRequest API to a JavaScript event listener and am able to track clicks on the button in a different Google Analytics account.

The JavaScript

 // URL of the POST request var url = "" var site = window.location.hostname

//Get the current unix time in JS and assign it to the cid var cid = (new Date).getTime();

// Parameters of the POST request notice the different UA var data = "v=1&tid=UA-40202040-2&cid=”+ cid + ”&t=event&ec="+site+"&ea=click&el=button"

//Event listener for clicks on the button document.getElementById("YOUR ID").addEventListener("ACTION",function() {

//function that loads after every click var xhr = new XMLHttpRequest();"POST", url, false); xhr.onload = console.log('loaded') xhr.send(data) //for debugging console.log("sent to " + data) },

false );


There is a full HTML/JavaScript example at the bottom of this post.

This gives you the ability to track things in your GA profile that happen on other sites.

What business questions does this answer?

1. What websites lead to the most interactions with my content pieces? 2. How do micro conversions change depending on the site?

Some things to keep in mind

1. When you implement this you are executing JavaScript on someone else’s page. This might be questionable for some bloggers because you can manipulate the DOM. Also, some of the big content management systems disable the use of JS for security reasons. In that case this would not work. Like I mentioned above the site/CMS must be able to execute the JavaScript that goes along with your asset.

2. You are limited in the amount of data you can send to GA. Here are the limitations for the free version of Universal Analytics:

200,000 hits per visitor per day 500 hits per session

Universal is now customer centric instead of visit centric which is why I made the cid the visitors’ current unix time. (If you are going to have really high traffic you can make this a random number since people might land on your site at the same exact time and be giving the same unix time id.) This allows you to not hit your hits per visitor per day limit since each visitor will have a unique customer ID. In addition, you will still be able to view the interactions on a customer level.

3. This will only tracking the interaction that happens on the page your asset loads. You could track other parts like page loads and referral traffic but that would get a little weird because you can push attribution to GA.

Full Page Example

Check it out here.

Check out these other posts about cool ways to use the Measurement Protocol 1 - Measuring twitter with Google Analtyics by Matt Stannard 2 - Tracking caffeine consumption with Universal Analtyics by LovesData 3 - Track your home with Google Analtyics by Me

What other cool ways have you seen the Universal Analytics Measurement Protocol used? Let me know if you have any questions in the comments.

If you liked this post, please consider sharing it. Also for more links, posts, thoughts and pics about SEO & analtyics follow me on twitter @NicoMiceli and G+

ANA_CTA_R1 (1)

We love helping marketers like you.

Sign up for our newsletter to receive updates and more: