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.
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.
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.
Make sure you set up the integration in Slack as an Incoming Webhook.
Server & Cron Jobs
You’ll need a server to run cron jobs on and a little knowledge of cron jobs.
Help! I don’t have a server!
Digital Ocean has a great $5/month plan that you can use.
Help! I don’t know how to add cron jobs!
Add the Cron Jobs
One to post the top real-time posts to Slack every 15 minutes, and another to post the daily digest once per day.
You can test without the cron jobs by running the toppostsrealtime.php script in the command line.
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.