Skip to content

Instantly share code, notes, and snippets.

@harssh
harssh / nginx
Created July 16, 2014 11:30 — forked from continuityapp/nginx
#! /bin/sh
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $all
# Required-Stop: $all
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
@harssh
harssh / .tmux.conf
Last active August 29, 2015 14:07 — forked from paulodeleo/.tmux.conf
# Make mouse useful in copy mode
setw -g mode-mouse on
# Allow mouse to select which pane to use
set -g mouse-select-pane on
# Allow mouse dragging to resize panes
set -g mouse-resize-pane on
# Allow mouse to select windows
@harssh
harssh / .tmux.conf
Last active August 29, 2015 14:07 — forked from snuggs/.tmux.conf
##############################
# _
# | |_ _ __ ___ _ ___ __
# | __| '_ ` _ \| | | \ \/ /
# | |_| | | | | | |_| |> <
# \__|_| |_| |_|\__,_/_/\_\
#
#############################
############################################################################

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:

/var/log/td-agent/td-agent.log {
size 200M
rotate 6
missingok
create 644 td-agent td-agent
compress
delaycompress
notifempty
sharedscripts
postrotate
# appサーバ用の設定
<source>
type tail
path /var/log/nginx/access.log
format apache
tag nginx.access
pos_file /var/log/td-agent/nginx.pos
</source>
<match nginx.access>

@hitsthings Your API is worth considering, without a doubt. My remarks:

  • It introduces new entities/method, which is not a big deal, but should be avoided if possible.
  • Does not support multiple parent schemas ("classes"). The latter of course can be easily fixed by using an array instead of a String value for _type(s).
  • I like that you're able to chose the property that holds the type information.
  • I think parent properties should be included by default, which is in line with programmers intuition.

I came up with an alternative approach that keeps the changes to the current API to a bare minimum and leverages the way models are created and cached.

Alternative approach

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//Database connection
var uristring = 'mongodb://localhost/test';
var mongoOptions = { };
mongoose.connect(uristring, mongoOptions, function (err, res) {
if (err) {
console.log('Error when connecting to: ' + uristring + '. ' + err);