Giter VIP home page Giter VIP logo

ga-extractor's Introduction

ga-extractor Build Status

Free your Google Analytics data

ga-extractor bulk-extracts Google Analytics (GA) raw data using the Core Reporting API. Use it as part of your ETL process.

ga-extractor was spun off from bower-server-etl, the ETL service that feeds Bower's registry.

Features

  • Compatible with latest GA Core Reporting API v3
  • Authorizes using OAuth 2.0 Service Accounts
  • Promises
  • Will break large extractions into smaller queries based on GA's single query limit
  • Well tested against live GA API server

Install

npm install --save ga-extractor

Usage

Sign up for a GA Service Account, download your .p12 private key.

var gaExtractor = require('ga-extractor');

var options = {
  clientEmail: "[email protected]",
  // need either keyPath or keyContent
  keyPath: "test/fixtures/xxx.p12",
  keyContent: "Bag Attributes...",
  // below are optional
  profileId: "xxxxxxxx", // define once here or define a different one in every queryObj
  impersonatedUser: "[email protected]"
  proxy: "http://proxy.example.com"
};

var gaExtractor = new GaExtractor(options);

// To build your query, see
// https://developers.google.com/analytics/devguides/reporting/core/v3/reference#q_summary
// and https://developers.google.com/analytics/devguides/reporting/core/v3/reference
var queryObj = {
  'start-date': '31daysAgo',
  'end-date': 'yesterday',
  'metrics': 'ga:users',
  'dimensions': 'ga:country'
  // no need to define 'max-results', always extracts all data
};

gaExtractor.extract(queryObj)
  .then(function (data) {
    // do something with data returned, e.g. transform & load into database
    data = [
      ["United States", "5471"],
      ["United Kingdom", "1084"],
      ["France", "801"]
      // ...
    ];
  })
  .catch(console.error);

To try your query without writing any code, use the Query Explorer Tool.

Example of original data returned by GA API:

{
  "kind": "analytics#gaData",
  "id": "...",
  "query": {
    "start-date": "7daysAgo",
    "end-date": "yesterday",
    "ids": "...",
    "dimensions": "ga:country",
    "metrics": ["ga:users"],
    "sort": ["-ga:users"],
    "start-index": 1,
    "max-results": 10000
  },
  "itemsPerPage": 10000,
  "totalResults": 137,
  "selfLink": "...",
  "profileInfo": {
    "profileId": "...",
    "accountId": "...",
    "webPropertyId": "...",
    "internalWebPropertyId": "...",
    "profileName": "...",
    "tableId": "..."
  },
  "containsSampledData": false,
  "columnHeaders": [],
  "totalsForAllResults": {
    "ga:users": "17205"
  },
  "rows": [
    ["United States", "5471"],
    ["United Kingdom", "1084"],
    ["France", "801"]
  ]
}

.extract returns only the content of rows object above.

Contribution

Install dependencies: npm install

Ensure tests pass: npm test

TODO: all tests are integration tests and execute against live GA API server. Docs on fixtures needed for testing coming soon. In the mean time you can submit a PR and owner will manually test.

Build .js for distribution: npm build

License

MIT © Ray Shan

ga-extractor's People

Contributors

mahmoudfelfel avatar rayshan avatar

Watchers

 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.