(Final up to date March 2014)
This submit is the ultimate chapter of a trilogy. The final word refinement, if you’ll. It began with my foray into the murky waters of context, after I examined how climate knowledge may very well be used to supply further details about website visits. After I wrote that submit, I had two trepidations: 1) does sending the API name with each single web page view have an effect on website efficiency negatively, and a couple of) does forcing the web page view name to attend for the API name to finish have an effect on the standard of go to metrics.
I mounted (1) to my satisfaction in a later submit on using a customized cookie to make the API name simply as soon as per session. This meant that website efficiency improved, because the API name (which could take as much as 500ms) is completed simply as soon as per go to. Additionally, it has a optimistic impact on go to calculation, since the opportunity of the web page view name failing to finish resulting from issues within the chain of occasions is minimized.
Nevertheless, I used to be nonetheless not proud of having any code executed earlier than the web page view, as a result of web page views are one thing you don’t need to compromise resulting from, for instance, issues with a third-party API (such because the climate API).
So I did what I ought to have been doing all alongside.
X
The Simmer Publication
Subscribe to the Simmer e-newsletter to get the most recent information and content material from Simo Ahava into your electronic mail inbox!
Customized dimensions may be despatched with occasions as nicely
Right here’s just a little background. I’ve a script which polls a climate API, with the customer’s location because the parameter. The script collects the climate situations of mentioned location, and passes them to a visit-scope customized dimension, which is then despatched to Google Analytics.
A customized dimension scoped for visits (or classes) signifies that you simply need to ship it as soon as per go to. All subsequent and former hits (web page views, occasions, transactions) of the go to are annotated with the dimension as nicely. Naturally, it’s a no brainer to thus ship the dimension simply as soon as per session, therefore the necessity for a customized cookie (once more, see this submit).
Additionally, since you may ship the identical customized dimension with an occasion, why even use a web page view? A non-interaction occasion does the identical factor PLUS you may evaluate the variety of climate occasions and visits from the identical time interval to see if there are discrepancies which point out issues with the API name.
One other perk, if utilizing GTM, is that you simply don’t want separate, complicated web page view tags (one with the climate dimension, one with out), however moderately you may have your fundamental web page view tag for monitoring and an occasion tag for sending the climate knowledge (which fires solely when the API name is made).
Implementation
Effectively, I must refer again to the code from my earlier two posts. Right here’s what the NEW customized HTML tag ought to appear to be (bear in mind, you must load jQuery and the geoPlugin service for this to work).
<script>
if (typeof({{Session alive}}) == "undefined") {
var lat = geoplugin_latitude();
var lon = geoplugin_longitude();
var climate = "";
var weatherAPI = "http://api.openweathermap.org/knowledge/2.5/climate?lat="+lat+"&lon="+lon;
$.ajax({
sort : "POST",
dataType : "jsonp",
url : weatherAPI + "&models=metric&callback=?",
async : true,
success : perform(knowledge) {
climate = knowledge.climate[0].essential ;
dataLayer.push({"climate": climate});
},error : perform(errorData) {
console.log("Error whereas getting climate knowledge :: "+errorData.standing);
dataLayer.push({"climate": "Undefined"});
},full : perform() {
dataLayer.push({"occasion": "weatherDone"});
}
});
}
var d = new Date();
d.setTime(d.getTime()+1800000);
var expires = "expires="+d.toGMTString();
doc.cookie = "session=1; "+expires+"; path=/";
</script>
Right here’s the order of occasions:
-
Test if a cookie named “session” exists
-
If cookie doesn’t exist, use the customer’s location to question the climate situations at mentioned location
-
Save climate situations in knowledge layer variable “climate”
-
Push occasion “weatherDone” to knowledge layer whether or not or not the API name is profitable
-
Lastly (no matter whether or not the cookie existed or not) reset the “session” cookie to run out in half-hour
So no want for a second occasion “noWeather” for the weatherless web page view. You possibly can simply have the conventional web page view despatched with each web page load, after which use “weatherDone” because the set off rule for the occasion (bear in mind to make it a non-interaction hit) which sends the “climate” variable as a customized dimension. Useless easy.
Lastly, you’ll must arrange your occasion tag. Keep in mind, you must create the customized dimension in Google Analytics’ admin part (see right here for a information). Make observe of the index quantity assigned to your new, session-level customized dimension.
Subsequent, create a brand new occasion tag. Now, you may have no matter you need as your occasion fields, however guarantee that Non-Interplay Hit is ready to True. On this instance, I ship my customized climate sort and temperature (they’re knowledge layer variable macros), however as you may see, Non-Interplay Hit is True. That is essential, as a result of then this climate occasion received’t have an effect on bounce price calculation (as a result of it doesn’t inform something about engagement now, does it?).
And right here’s what the customized dimension settings appear to be. You set them ultimately of the tag, beneath Extra settings.
Conclusion
I feel I’ve received the entire thing pinned down now. I’ve provide you with a way to
-
ballot exterior APIs for contextual data
-
ship this data simply as soon as per go to
-
use a non-interaction occasion to annotate the go to hits with this dimension
Subsequent steps can be to ensure the API is dependable and quick. Additionally, possibly some type of timeout set off for the API name ought to be in place, with a “climate”: “Undefined” worth despatched if the API name doesn’t full.