Skip to content

Instantly share code, notes, and snippets.

Consuming Google Analytics data from a NodeJs application using a service account

I didn't have a great time trying to implement a simple feature involving Google Analytics in one of my projects (nodejs web application).

My humble requirement was to collect some data (events) I was pushing to Google Analytics in order to create a simple report for my users. Since it isn't a critical report, my idea was to get the data once a day and cache it.

So, as simple as it seems, it has been a tough trip for me and I want to share the whole process with the community. I hope anyone trying to achive something similar gets to this article and find it helpful.

These are the main steps I had to take:

#!/bin/bash
# nodejs - Startup script for node.js server
# chkconfig: 35 85 15
# description: node is an event-based web server.
# processname: node
# server: /path/to/your/node/file.js
# pidfile: /var/run/nodejs.pid
#
#!/bin/sh
# ------------------------------------------------------------------------------
# SOME INFOS : fairly standard (debian) init script.
# Note that node doesn't create a PID file (hence --make-pidfile)
# has to be run in the background (hence --background)
# and NOT as root (hence --chuid)
#
# MORE INFOS : INIT SCRIPT http://www.debian.org/doc/debian-policy/ch-opersys.html#s-sysvinit
# INIT-INFO RULES http://wiki.debian.org/LSBInitScripts
# INSTALL/REMOVE http://www.debian-administration.org/articles/28
#!/bin/sh
#
# chkconfig: 35 99 99
# description: Node.js /home/nodejs/sample/app.js
#
. /etc/rc.d/init.d/functions
# Creamos un fichero PID para monit
SCRIPT="$(basename $0)"
The objective of this post is to get you from absolutely nothing, to a fully functional nodejs environment.
Software used: Ubuntu 11.10, Nodejs v0.6.12, Nginx, MongoDB, Redis, and NPM modules.
1. Download and install the latest version of Ubuntu: http://www.ubuntu.com/download (don't select any extra items to install when prompted)
2. Once you are logged in and are at your Ubuntu command prompt, install the necessary software you will need:
a. sudo apt-get install openssh-server
b. sudo apt-get install libssl-dev
c. sudo apt-get install git
d. sudo apt-get install g++
e. sudo apt-get install make
import (
"io"
"net/http"
)
//With HandlerFunc
func main () {
http.Handle("/", JsonpHandler(http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
w.Write("Hello World")
})))
@mrocha-me
mrocha-me / gulpfile.js
Last active August 29, 2015 14:22 — forked from yhsiang/gulpfile.js
var gulp = require('gulp'),
gutil = require('gulp-util'),
stylus = require('gulp-stylus'),
uglify = require('gulp-uglify'),
jade = require('gulp-jade'),
concat = require('gulp-concat'),
livereload = require('gulp-livereload'),
tinylr = require('tiny-lr'),
express = require('express'),
app = express(),
@mrocha-me
mrocha-me / gulpfile.js
Last active May 13, 2020 02:04
Gulp - Project to build site with Jade + Stylus with Livereload & Watch.
/*
Created by: Mauricio Rocha
www.sampapix.com.br
Install the plugins below, use -global or --save-dev either way will work.
gulp, gulp-jade, gulp-concat, gulp-uglify, gulp-minify-css, gulp-stylus, gulp-obfuscate, gulp-connect
Create the folders below inside your project folder:
src, src/js, src/styl
@mrocha-me
mrocha-me / gist:016a37dd0557540ef6a0
Created June 9, 2015 20:16
Random hex colors in Javascript.
/* Random hex colors in javascript. */
(Math.random() * 0xFFFFFF >> 0).toString(16);
@mrocha-me
mrocha-me / index.html
Last active August 29, 2015 14:23 — forked from zentrope/index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<meta name="msapplication-tap-highlight" content="no" />
<!-- WARNING: for iOS 7, remove the width=device-width and
height=device-height attributes. See
https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport"