Integrating Slack and Google Analytics

Evan Sobkowicz
Nov 19, 2015 · 2 min read

This post dives into how I hacked together a Slack integration for The Ithacan. If you’re interested in how The Ithacan is using Slack, see my previous post.

The Goal

The end goal of the following steps is to automatically post frequent real-time updates from Google Analytics to a Slack channel. Not all of our editors want to look at the mess that is Google Analytics, so it’s easier for them to be able to see select data in Slack.

The finished Slack channel

Initial Code

I started by adventuring across the internet with some help from my good friend, Google. I came across some services that claimed to do some sort of Google Analytics/Slack integration, but didn’t find anything that looked like what I wanted (and free).

I found this GitHub repository: Google Analytics Slack notifications. There were two problems though. 1) It was last updated over a year ago in October 2014. 2) I struggled to get it to work because of some bugs in the code and the lack of response and action on this issue.

Working Code

You can find a functional version of the code here: https://github.com/evansobkowicz/slack-analytics

You’ll need to create a Google Developer Project to get access to their API. There are instructions in the README of the repository on how to do this.

Use the appropriate data from your account in setup.php and also put the privatekey.p12 file in the same directory as setup.php.

Slack Integration

Make sure you set up the integration in Slack as an Incoming Webhook.

Slack webhook integration setup screen

Server & Cron Jobs

You’ll need a server to run cron jobs on and a little knowledge of cron jobs.

Digital Ocean has a great $5/month plan that you can use.

Reading: Wikipedia and/or Digital Ocean’s support article.

One to post the top real-time posts to Slack every 15 minutes, and another to post the daily digest once per day.

Testing

You can test without the cron jobs by running the toppostsrealtime.php script in the command line.

That’s it!

That should be everything. Once it’s working you should see updates automatically posting in your targeted channel with your website stats.


Please let me know if you have any questions, feedback, or cool Slack tips! Connect with me on Twitter: @evansobkowicz.

    Evan Sobkowicz

    Written by

    Software Engineer