Sending Google Analytics Data From Meteor.js Server Side

| Comments

Recently I’ve created a website that responds with HTML and also has an API that responds with JSON data. The website was create in Meteor.js and I wanted to integrate it into Google Analytics. Having it send Google Analytics statistics on the client side was easy and straight forward. However when it came time to send statistics to Google Analytics from the Meteor.js server side of the application I had to spend quite a bit of time finding an answer.

Use the Google Analytics Measurements Protocol

Check out the Google Developers section on Google Analytics regarding the Measurements Protocol.

Use the Meteor function

Don’t forget, as of Meteor 1.2 this isn’t on by default. It needs to be added by doing the following from the command prompt:

meteor add http

Putting it all together

Here are the results of my code in order to trigger a page view every time the user visits the /api route of my web app.

if (Meteor.isServer) {
  Meteor.startup(function () { {
        this.route('methodExample', {
            path: '/api',
            where: 'server',
            action: function() {

              // Send Google Analytics
      "POST", "",
                           {params: {
                              'v': '1',
                              'tid': 'UA-XXXXXX-Y',
                              'cid': '555',
                              't': 'pageview',
                              'dp': 'api'

coding, meteor, webdev