grails (11) vaadin (11) meteor (6) java (4) elasticsearch (3) apple (2) centos (1) cloudbees (1) google analytics (1) gradle (1) heroku (1) javafx (1) javascript (1) jdbc (1) jug (1) logback (1) logging (1) mac os (1) management (1) mongodb (1) mongolab (1) mysql (1) twitter (1) ubuntu (1)

Tuesday, May 21, 2013

Meteor with Google Analytics

Trying to get Google Analytics working with Meteor framework is so much fun. It took me quite some after I got this wonderful view:

How to get there: that is the question which is answered in the following gist.

Create new template, so we can hook to Template.googleAnalytics.rendered function. So, when googleAnalytics template is rendered, we can call Google Analytics service.
<template name='googleAnalytics'>
</template>
Insert the template into the HTML page that should be reported to Google Analytics.
<body>
{{> googleAnalytics}}
</body>
view raw index.html hosted with ❤ by GitHub
Implement the call to Google Analytics service as follows (just replace 'UA-111111-1' with your token).
Template.googleAnalytics.rendered = function() {
new GA('UA-111111-1');
}
GA = function(code) {
var _gaq = window._gaq || [];
_gaq.push(['_setAccount', code]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
};
Thanks to these two sources.
TextLab for Mac
Ultimate application to validate, clean and format JSON, XML, SQL, HTML.

No comments:

Post a Comment