Giter Site home page Giter Site logo

google-analytics-proxy's Introduction

Google Analytics Proxy

Introduction

YQL is a service that enables you to access and combine multiple sources of publically available data via a SQL-like syntax.

The YQL GA Proxy allows you to publish data from the Google Analytics API via a publically accessible REST url. You can use the webservice to create custom dashboards, widgets, product/search recommendation or autosuggest algorithms. The service works with the core reporting, real time reporting and multi-channel funnels reporting APIs.

Features

Scalability

The YQL GA Proxy handles all authentication so you do not need to use the Google Python or Java OAuth APIs. It is therefore possible to create a purely client side application without any server side components.

Caching

You can use YQL to cache any requests. By utilising YQL's caching infrastructure, you can cache requests to effectively manage your quota.

Mixins

One of the major benefits of using the YQL GA Proxy is that you can merge the data from the GA API with other data sources. As an example, you can create a recommendations algorithm that merges data from the GA API with products that have also been shared on social media platforms.

Assumptions

This app assumes you have the following:

  • A basic knowledge of the Google Analytics and YQL APIs
  • A Google account
  • A Yahoo account
  • A registered Google Analytics API project and the associated environment variables

Overview

A sample user flow would be:
  • User obtains a refresh token
  • User exchanges the refresh token for an access token via the Google OAuth API (the refresh token is valid for 60 mins and will be cached for this period of time)
  • User is able to make authenticated requests to the GA API
Getting started
  • After you've created an API project, make a note of the following for your project:

  • 'client_id' - should always be set to the value in the API console

  • 'client_secret' - should always be set to the value in the API console

  • 'refresh_token' - should always be set to the value in the API console

  • 'grant_type' - should always be set to 'refresh_token'

  • 'scope' - should always be set to 'https://www.googleapis.com/auth/analytics.readonly'

  • Use the GA Dev Tools Explorer (http://ga-dev-tools.appspot.com/explorer/) to get a valid query URI to use.

  • Initiate a request from your application as per below. Note, you will need to replace the values above with their correct values:

http://query.yahooapis.com/v1/public/yql/
q: 'USE "path_to/google.analytics.xml" as ga; SELECT * from ga where auth IN (SELECT access_token from ga where client_id = 'client_id' and client_secret = 'client_secret' and refresh_token = 'refresh_token' and grant_type = 'grant_type' and scope = 'scope') and ids = @ids and metrics = @metrics and start-date = @start-date and end-date = @end-date;'
format: 'json',
ids: 'ga:123456',
metrics: 'ga:visitors',
start-date: '2012-10-04',
end-date: '2013-10-04'
  • You'll notice that thus request works correctly but all the environment variables are visible to any users. Instead, we should use query aliases to hide all environment variables.

Using YQL query aliases

USE "path_to/google.analytics.xml" as ga; SELECT * from ga where auth IN (SELECT access_token from ga where client_id = 'client_id' and client_secret = 'client_secret' and refresh_token = 'refresh_token' and grant_type = 'grant_type' and scope = 'scope') and ids = @ids and metrics = @metrics and start-date = @start-date and end-date = @end-date;
  • Select an alias and click 'next'

  • The YQL console will now show you the generated URL, e.g:

http://query.yahooapis.com/v1/public/yql/{{username}}/{{query}}
  • To use the query, you simply need to initiate a request from your application as per below, e.g.:
http://query.yahooapis.com/v1/public/yql/{{username}}/{{query}}
format: 'json',
ids: 'ga:123456',
metrics: 'ga:visitors',
start-date: '2012-10-04',
end-date: '2013-10-04'

Caching requests

  • To cache a request, you simply need to append the '_maxage' parameter to the URL. The parameter value should be populated with the length of time that the request should be cached for, e.g.:
http://query.yahooapis.com/v1/public/yql/{{username}}/{{query}}
format: 'json',
ids: 'ga:123456',
metrics: 'ga:visitors',
start-date: '2012-10-04',
end-date: '2013-10-04',
_maxage: 3600

Using the real time API

  • To use data from the realtime API, you should change the query to use the 'google.analytics.realtime.xml' file and repeat the steps in the 'Overview', 'Using YQL query aliases' and 'Caching requests' sections.

An example is shown below:

http://query.yahooapis.com/v1/public/yql/
q: 'USE "path_to/google.analytics.realtime.xml" as ga; SELECT * from ga where auth IN (SELECT access_token from ga where client_id = 'client_id' and client_secret = 'client_secret' and refresh_token = 'refresh_token' and grant_type = 'grant_type' and scope = 'scope') and ids = @ids and metrics = @metrics and start-date = @start-date and end-date = @end-date;'
format: 'json',
ids: 'ga:123456',
metrics: 'ga:visitors',
start-date: '2012-10-04',
end-date: '2013-10-04'

google-analytics-proxy's People

Contributors

alexroutledge avatar

Stargazers

 avatar Alexsandro Souza Pereira avatar Tim Elfelt avatar

Watchers

James Cloos avatar Alexsandro Souza Pereira avatar Kaushan  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.