Everybody who ever pays for traffic should be using campaign tracking! How else will you ever find out whether you spend your money wisely?
But how about monitoring a campaign in real time?
Well, Analytics has “Real-Time” — three “reports” or maybe “dashboards” that show data very quickly and refresh automatically.
- Have a dashboard on a screen that gives you a quick overview of what’s happening on your site right now, or
- Monitor the result of something you did — launch a campaign for example
There is just one small issue: because data comes so quickly, there is no time to process it. What you see in the Real-Time reports is raw data. Which means: no persistence.
If you select a dimension that would normally persist, you get a warning
But the second paragraph instills hope. There is a way.
We need to add two things to our setup:
- save the tracking code somewhere when we get it, and
- retrieve the tracking code when the conversion happens, and track it.
There a lot of ways to save and retrieve values on a site, the most straight-forward being: put it into a cookie. So that’s what we’ll do. You can also use localStorage or your session store, if you want.
For the remainder of this post, I’ll presume your tracking code comes in a URL parameter named “cid”, and that your conversion is a purchase (i.e. we’re looking for the “purchase” event).
Workaround w/o DTM
Both steps mentioned above can be easily handled by changing the doPlugins()
method. Which is good, right?
The first piece of code that we need has to store the tracking code if there is one.
// step 1 - if there is a tracking code, store it if (s.campaign) { // save it! s.c_w("s_tcstore", s.campaign); console.log("Saving tracking code '" + s.campaign + "' for later..."); }
The second piece of code we need has to read the tracking code and track it, but only if there was a conversion.
// step 2 - if this is a conversion, use the stored tracking code if (s.events.indexOf("purchase") >= 0) { // load tracking code var tc = s.c_r("s_tcstore"); if (tc) { // might be empty // track it s.prop6 = tc; console.log("Sent tracking code '" + tc + "' into prop6"); } }
If you put those into your doPlugins()
method, it should look like this:
And the Real-Time report is agreeing with me.
Workaround with DTM
What if you’re using DTM?
Well, the two parts of the Javascript above translate into two Page Load Rules. Rule 1 will handle the storage of the tracking code, while rule 2 will retrieve and track it.
Rule 1 — storage of tracking code
As above, we want this PLR to store the tracking code, if there is one.
The “if there is one” part translates into a condition within the rule.
- Best — “Data Element” — this one is the most stable and will work in a wide variety of situations.
- Ok — “URL Parameter” — this one only works if your tracking code is in a single URL parameter (like the “cid” that I suggested above), but it won’t really work for Google-style “utm_xyz” parameters, at least not 100% reliably.
- Ok — “Custom” — flexible and versatile if you know what you’re doing.
- Not so good — “JS Variable” — this one will only work if your Javascript variable is set before this rule fires. In most deployments, that would not be the case.
Wondering about the “.+”?
Storing the tracking code into a cookie can be done with Javascript, as above, but this time it is a bit easier, I think.
Using the “Add New Script” button in the “Javascript / Third Party Tags” section, create a new script and put this into it:
_satellite.setCookie("rttc", _satellite.getVar("Campaign Tracking Code"),0);
Yup, that’s it.
Rule 2 – tracking of stored tracking code
Before we make this rule, let’s make a Data Element that reads the cookie.
First, the conditions for the rule should be two-fold:
Now we need to assign the value.
An alternative approach could be to incorporate the storing of the tracking code into your standard page load rule. And you could absolutely use the DE for the stored tracking code in your standard “Thank you page” PLR.
I’ll leave that one to you, dear reader, as an exercise.
NIce explain of using cookies in DTM
LikeLike