Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/segment-boneyard/google-analytics
Query Google Analytics from node
https://github.com/segment-boneyard/google-analytics
Last synced: about 5 hours ago
JSON representation
Query Google Analytics from node
- Host: GitHub
- URL: https://github.com/segment-boneyard/google-analytics
- Owner: segment-boneyard
- Created: 2014-03-07T02:04:41.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-03-07T23:23:17.000Z (over 10 years ago)
- Last Synced: 2024-04-09T16:31:05.166Z (7 months ago)
- Language: JavaScript
- Size: 129 KB
- Stars: 31
- Watchers: 39
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: Readme.md
- Changelog: History.md
Awesome Lists containing this project
README
# google-analytics
Query Google Analytics data from node.
## Installation
$ npm install segmentio/google-analytics
## Example
Create a new Google Analytics instance with a `viewId`, and then login:
```js
var ga = require('google-analytics')('67032470');ga.login('[email protected]', 'password', function (err) {
// login first
});
```Then you can query unique visitors:
```js
ga.query()
.visitors()
.weekly()
.start(new Date('1/1/2014'))
.end(new Date('2/1/2014'))
.get(function (err, res) {});
```Or form a generic query:
```js
ga.query()
.dimension('ga:source')
.metric('ga:visits')
.filter('ga:medium==referral')
.segment('segment=gaid::10 OR dynamic::ga:medium==referral')
.start(new Date('1/1/2014'))
.end(new Date('2/1/2014'))
.index(10)
.results(100)
.get(function (err, res) {
// ..
});
```The [data feed request format](https://developers.google.com/analytics/devguides/reporting/core/v2/gdataReferenceDataFeed) explains the different arguments that can be used in a Google Analytics data request. You can play around with sample requests in the [GA dev tools query explorer](http://ga-dev-tools.appspot.com/explorer/?csw=1).
### Custom Dynamic Segments
You can create powerful dynamic segments on demand using [filter operators](https://developers.google.com/analytics/devguides/reporting/core/v3/reference#filterOperators), like so:
```js
ga.query()
.visitors()
.daily()
.start(start)
.end(end)
.segment('dynamic::ga:pagePath==/,ga:pagePath==/signup,ga:pagePath==/pricing,ga:pagePath=@/docs')
.get(function (err, res) {
if (err) return callback(err);
var visitors = res.rows.reduce(function (memo, row) {
return memo + parseInt(row[1], 10);
}, 0);
callback(null, visitors);
});
```## License
MIT