Fetch Your Google Analytics Data Using Rails
Today we will see how to collect your data from Google Analytics. Everyone use it, I’m sure, but sometime, for many different reasons, you can have to collect those data using the API provided by google. Of course with rails most of the time you can find a gem to do that and for Google Analytics API you have garb.
This gem allows you to connect to your google account (or the one of your customer) and then fetch all the information you need.
In theory it’s not so complicated, but in practice if you want to provide a real analytics engine, this gem in not so convenient.
Let’s see how to implement it in a basic way first. Let’s say I just want all the time the same metrics, the number of visits per day during the last month.
First step connect and fetch the google profile you need (the one with UA-XXXXXXXX-XX).
1 2 3 4 |
|
Second step create the report you want
1 2 3 4 5 6 |
|
And then fetch the results
1
|
|
So this is not so bad but let’s say you want to have a report now with the number of new visitors per days, then also per month, the visits per hours and just even let the user select the metric, the dimension and even some filter (s)he want. You will not be able to write a report for everything so let’s adapt this to create a dynamic report.
A generic report
First you need to add the reset function in ReportParameter (this class manage a list of elements which can be your metrics or your dimensions).
1 2 3 4 5 6 7 |
|
When this is done, you can remove all your reports files and write only one.
1 2 3 |
|
And now start the “tricky” part. Instead of just Report::Visits.result()
we will have to create a report and add the metrics and dimensions just like that.
1 2 3 4 5 6 |
|
Once this is done you can fetch your result like before
1 2 |
|
For filters you need to give it on the result function like that
1 2 3 |
|
Of course this is not really convenient if you want to let the user choose its own filter because you need to play with some symbols but with a small function you can convert all.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
(you can see some meta-programming with method(comparator).call
this will call the method which have the name of the value of comparator)
Now you can simply create a controller to give all your parameters and then just create the report you want.
1 2 3 4 5 6 7 8 9 10 |
|
Now you can create a nice UI to display all possible metrics and you can do your clone of Google Analytics (or maybe not…). You will see the API is not really fast so you will need to use a lot of cache or display a nice loader for your user ;)
Note: Of course you can write your report(s) as a model but personally I prefer to create a folder for that and then load it in my config/application.rb with config.autoload_paths += %W(#{config.root}/app/reports)