Skip to main content
Skip table of contents

Example import from Google Analytics REST API

Create new source application

Go to Source Data tab and Add new source application and select REST API application type.

REST API source parameters

In the next step, you will need to provide REST API source parameters which will be used to retrieve the data. See example:

In Source data URL provide customized Google Analytics REST API URL which returns the data (https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXX&start-date=2016-10-01&end-date=2017-11-30&metrics=ga%3APageLoadTime&dimensions=ga%3Adate). Change at least following URL parameters (corresponding to Google Analytics API Reference Guide):

  • ids - The unique table ID of the form ga:XXXX, where XXXX is the Analytics view (profile) ID for which the query will retrieve the data.
  • metrics - A list of comma-separated metrics, such as ga:sessions,ga:bounces.
  • dimensions - A list of comma-separated dimensions for your Analytics data, such as ga:browser,ga:city.

To reduce server load Google Analytics REST API provides pagination, default by 1000 records.

Choose Pagination option "Offset and limit parameter" and enter following parameters:

  • Offset parameter - start-index
  • Limit parameter - max-results

To get Client ID and Client secret you must create web application credentials in API console Credentials page (more information can be found here - https://developers.google.com/identity/protocols/OAuth2WebServer#creatingcred)

Please specify there Authorized redirect URIs as Redirect URL that you see in flex.bi REST API source parameters page.

After registration you will get your application Client ID and Client Secret - please paste them in flex.bi REST API source parameters page corresponding fields.

If you will create several Google Analytics REST API sources in your flex.bi account then you can use the same Client ID and Client Secret for all these REST API sources.

Restrict requested data set by providing Scope value - https://www.googleapis.com/auth/analytics.readonly

Fill Authorize URL with "https://accounts.google.com/o/oauth2/auth?access_type=offline" (access_type=offline is a must to get refresh token from Google Analytics REST API) and Token URL with "https://accounts.google.com/o/oauth2/token"

Click Add custom JavaScript code to add custom JavaScript code that will process received data:


CODE
function formatDate(s) {
return s.substr(0,4)+'-'+s.substr(4,2)+'-'+s.substr(6,2);
}
 
return _.map(doc.rows, function(row) {
var rowdoc = {};
for (var i = 0; i < doc.columnHeaders.length; i++) {
var name = doc.columnHeaders[i].name.replace(/^ga:/, '');
rowdoc[name] = name == 'date' ? formatDate(row[i]) : row[i];
}
rowdoc["Project"] = "Project 1";
return rowdoc;
});

You can add own custom/calculated values, like in this example value for "Project" property.

Enable Analytics API

Next you need to enable Analytics API. In order to do that you must go to console Dashboard page, click on the "ENABLE APIS AND SERVICES" and add "Analytics API" to the list:

Authorize access to REST API source

In the first time you will be redirected to source application authorization page where you will need to authorize flex.bi access to source application data.

Source columns mapping

Example of Google Analytics import column mappings


Google Analytics source application definition example

If you would like to use this Google Analytics import example from this tutorial then you can use the following source application definition:

Expand source
CODE
{
  "application_type": "rest_api",
  "application_params": {
    "source_params": {
      "url": "https://www.googleapis.com/analytics/v3/data/ga?ids=ga:XXXX&start-date=2016-10-01&end-date=2017-11-30&metrics=ga%3APageLoadTime&dimensions=ga%3Adate",
      "skip_ssl_verification": "0",
      "pagination": "offset_limit",
      "authentication_type": "oauth2",
      "content_type": "json",
      "oauth2_scope": "https://www.googleapis.com/auth/analytics.readonly",
      "oauth2_authorize_url": "https://accounts.google.com/o/oauth2/auth?access_type=offline",
      "oauth2_token_url": "https://accounts.google.com/o/oauth2/token",
      "custom_javascript_code": "function formatDate(s) {\n  return s.substr(0,4)+'-'+s.substr(4,2)+'-'+s.substr(6,2);\n}\n\nreturn _.map(doc.rows, function(row) {\n  var rowdoc = {};\n  for (var i = 0; i < doc.columnHeaders.length; i++) {\n    var name = doc.columnHeaders[i].name.replace(/^ga:/, '');\n    rowdoc[name] = name == 'date' ? formatDate(row[i]) : row[i];\n  }\n  rowdoc[\"Project\"] = \"Project 1\";\n  return rowdoc;\n});",
      "offset_parameter": "start-index",
      "limit_parameter": "max-results",
      "limit_value": 1000
    }
  },
  "source_cube_name": "GA_TEST3",
  "columns": [
    {
      "name": "date",
      "data_type": "date",
      "dimension": "Time"
    },
    {
      "name": "PageLoadTime",
      "data_type": "integer"
    },
    {
      "name": "Project",
      "data_type": "string"
    }
  ]
} 

Troubleshooting

If you have some issues please read analytics API troubleshooting section. To reset the authorization you can remove your app from your google account.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.