Is it possible to import data from Google Analytics?

Since Mathematica 10 it's possible to pull data from Google Analytics using ServiceConnect and ServiceExecute. I refer you to the documentation for all the details, but here is a small sample.

Establish a connection:

ga = ServiceConnect["GoogleAnalytics"]

Get the number of visitors and the number of sessions over the last ten days:

req1 = ServiceExecute[ga, "ReportData", {
   "ProfileID" -> "108086866",
   "StartDate" -> Now - Quantity[10, "Days"],
   "EndDate" -> Now,
   "Metrics" -> {"Users", "Sessions"}
   }]

Mathematica graphics

Note the parameter that's called ProfileID. In order to retrieve this parameter you have to log into the web interface and select the website that you want to retrieve data from. Currently the end of the URL is formatted like this:

a<string of numbers>w<string of numbers>p<string of numbers>

The string of numbers that comes after p is the ProfileID.

The previous query returned the total number of visitors and the total number of users, what if we want to get a time series? We can use the parameter Dimensions to select the resolution that we want:

req2 = ServiceExecute[ga, "ReportData", {
   "ProfileID" -> "108086866",
   "StartDate" -> Now - Quantity[10, "Days"],
   "EndDate" -> Now,
   "Metrics" -> {"Users", "Sessions"},
   "Dimensions" -> "Day"
   }]

Mathematica graphics

Plotting it:

ListLinePlot[
 ToExpression@Normal@req2[All, {#Day, #Users} &]
 ]

Mathematica graphics


I just logged into Google Analytics and had a look at the export formats they offer. They offer many different formats that can be read by Mathematica: CSV, TSV and Excel are those that stand out as very easy to work with.

Here is an example where I chart the number of visitors over a certain period of time:

data = ImportString["Day Index,Visits
    12/11/13,374
    12/12/13,359
    12/13/13,301
    12/14/13,259
    12/15/13,325
    12/16/13,365
    12/17/13,302
    12/18/13,334
    12/19/13,284
    12/20/13,256
    12/21/13,279
    12/22/13,283
    12/23/13,219
    12/24/13,158
    12/25/13,308
    12/26/13,330
    12/27/13,327
    12/28/13,293
    12/29/13,354
    12/30/13,339
    12/31/13,210
    1/1/14,409
    1/2/14,415
    1/3/14,357
    1/4/14,351
    1/5/14,414
    1/6/14,471
    1/7/14,485
    1/8/14,492
    1/9/14,420
    1/10/14,368", "CSV"][[2 ;;]];

values = Transpose[data][[2]];
labels = Riffle[Rotate[#, Pi/4] & /@ Transpose[data][[1, 1 ;; ;; 2]], ""];

BarChart[values, ChartLabels -> labels]

enter image description here

In your case you had several different stats in the same CSV file. But the procedure is still simple: load the data, inspect the list that you get, use Part to select the data you're interested in, and so on. If you're very new the list manipulation may be cumbersome for you, but at the same time it will be great practice.


Have not checked this but it seems to cover your question as well. http://code.google.com/p/ragfield-packages/source/browse/GoogleAnalytics.m

regards Patrick