WTF is a Macro? Passing Variables to Tracking Code

A marketing company says they want to pass a "macro" to their tracking script, and that only I can do it. Tag Manager and JavaScript are the answer.

I've never pretended Analytics was my forte; I can handle it, but it's not where most of my energy winds up directed. Like all developers, though, I'm a pro at leveraging Google and StackOverflow to fill in the gaps. Still, every now and then, these vast fonts of wisdom fail to deliver and I come that much closer to developing chronic ulcers.

Here's some background for this post: I receive communication that Quantcast wants me to deploy a tracking tag to a specific site page and that they want me to pass a "macro" into the code. Specifically, the say they want their code to gather a "Form fill ID". Maybe it's because tracking isn't really my thing, but this means nothing to me: nada, zilch, zippo--hence the title for this post.

"A 'Form fill ID', huh?... like we need to take the form's ID attribute (i.e.; <form id="myform">) and pass its value to the Quantcast tag running on the form submitted page? Or is it that they want to pass actual form selections to their script (creepy)? I better send these guys a quick e-mail just to be sure..."

So, this 'Form fill ID'--is this an arbitrary value, or is it supposed to be fixed?--are you wanting to pull it from the form? What exactly are you guys looking for? I see you guys left some placeholders in your tag, which one did you need me to insert it into?

I get some fanciful business back about how forms magically create a variable which Quantcast calls a "Form fill ID" and that I should be able to pass it into their tracking code using developer kung fu. In other words, they don't know what the variable looks like on my end; nothing about where it should enter their code, either. Thanks.

After some time on Google, I gather that a macro is just a fancy Analytics term for what I understand to be a variable (var = foo). That variable (which, say, represents some piece of data on a page) can be inserted into the tracking script and passed on as analytics data to the tag owners.

Not being one to shy away from a challenge, though, I have a look at the Quantcast tag one more time; it looks something like this:


var _qevents = _qevents || [];

(function(){
var elem = document.createElement('script');
elem.src = (document.location.protocol == 'https:' ? 'https://secure' : 'http://edge') + '.quantserve.com/quant.js';
elem.async = true;
elem.type = 'text/javascript';
var scpt = document.getElementsByTagName('script')[0];
scpt.parentNode.insertBefore(elem, scpt);
})();

_qevents.push({qacct:'p-INSERT+PCODE',
labels:'_fp.channel.INSERT+ADDITIONAL+INFO+1,_fp.subchannel.INSERT+ADDITIONAL+INFO+2,_fp.event.Purchase Confirmation,_fp.customer.INSERT+CUSTOMER+TYPE
,_fp.pcat.INSERT+PRODUCT+CATEGORY',
orderid:'INSERT+ORDER+ID',
revenue:'INSERT+REVENUE',
event:'refresh'}
);

The macro placeholders are pretty obvious, and the one that caught my attention was the Order ID placeholder (bolded above). I figure that Quantcast is just inconsistent with their terminology (tech and marketing support using different vocab) and that they need me to pass this "Form Fill ID" (whatever that is) into the Order ID placeholder. When I think "Order ID" it could either be fixed (i.e.;  representing a specific sales campaign) or it could be arbitrary (i.e.; representing a unique order number). Interesting... 

It's at this point that I also, out of curiosity, look at Google Tag Manager to see if Quantcast's tag is in the Tag Manager library. Sure enough, it is.

Quantcast tag setup on Google Tag Manager
(Why hasn't Google been sued by Lego for their use of bricks here?)... See that Order ID field?--me thinks it needs a value!

Whats more, Quantcast has great Tag Manager documentation; there's even a decent section detailing data pass-back (in other words, "macros") which covers setting up a JavaScript dataLayer. The dataLayer property is a pretty cool device; it allows you to setup a global array of key/value pairs that would go in the head of your page. As I understand it, since the array is global it can be passed into the tracking code. Quantcast gives an example of the following keys & values:


dataLayer = [{
  'orderid': '123456789',
  'revenue': '99.99'
}];

With a little tweaking, I'm sure this could be adapted to pass dynamic data based on user interaction with the site. The coolest part is that either of those keys (i.e.; 'orderid' & 'revenue') could  be passed into the tag from within Tag Manager by creating a Data Layer Variable--no need to get your hands dirty with site files.

Very posh and all, but it still really doesn't answer my burning question: should the value be fixed or arbitrary?

Not wanting to put more work into this than necessary, I says to myself, "Let's go with a fixed value; it'll take 2 minutes to setup in Tag Manager -- if they don't like it I'll follow up."

A fixed value is a breeze: Open up Tag Manager, head to the Variables menu and add a "new" User-Defined Variable. When prompted to choose a variable type, select "DataLayer Variable". From there you can declare the default value:

Declaring a fixed dataLayer value
For this example, I've set a fixed default value of "MyFormID"

This is basically the same as going into the head and adding this, where 'formfillid' is the key and 'myformid' is the value:


dataLayer = [{
  'formfillid': 'myformid',
}];

Painless--didn't even break a sweat. Before we get back to the Quantcast tag, take note of the name we've given this variable (top left of the image above). We'll want to reference that in the Quantcast tag setup. So, head back to your Quantcast tag, click on the little Lego block next to the Order ID field; you can now select a variable to pass into that field. You'll see the Data Layer Variable you just setup in the list of variables to choose from. Select it and you should be good to go:

Insert a dataLayer variable into a Quantcast tag
Tag manager adds the double brackets once you select the dataLayer variable. I suppose you could type this in yourself if you remember the name of your variable.

Save your tag--preview before pushing if you like--and you now have a fixed value Key/Value pair that goes from your dataLayer straight into your tracking code. The marketing team now has one more piece of trivial information with which they can remarket their leads to death. The world is safe for another day...

... or is it?

Followup from Quantcast:

Thanks!--but, that's not what we needed. We need a way to distinguish one form submission from the other, so some sort of unique number or letter string would be great.

I have words... they will go unspoken here.

Now, this isn't a big deal, actually. If we can pull a dataLayer variable into the tracking code with Tag Manager, we should also be able to pull in some custom JavaScript that returns an ARBITRARY value. The script doesn't need to be anything special, so I'm going with a random number generator: with enough place values, the numbers should be unique; it's good enough for me. Each time the tag loads it will pull in a random number returned by our script and pass that value into our Order ID field.

 The first thing we'll do is go back to the Variables menu in Tag Manger. We'll add a New variable and this time when prompted to choose a variable type we'll select "Custom JavaScript" from the menu (you should see it right above "Data Layer Variable"). The rules for Custom JavaScript variables are pretty straightforward: your script has to return a value using the return() statement--that's it. So something like this should do:


function() {
  //variable for random number:
  var num = Math.floor((Math.random() * 100000000) + 1);
  //quantcast wants the number in single quotes:
  var quotes = "\'" + num + "\'";
  return(quotes);
}

Save the Custom JavaScript variable, and take note of the name. We head back to our Quantcast tag and this time around, in the Order ID field (click on the LEGO brick) we'll select the Custom JavaScript variable instead of the Data Layer variable. Save it, publish it, and we're good.

This was the end of this ticket. Quantcast was happy with the solution.

These are some pretty simple applications of what are some otherwise powerful tools in Tag Manager. I'll admit to being ever so smitten. I don't have all that much occasion to open up Tag Manager, but I'm glad this ticket came my way.