UNPKG

4.23 kBMarkdownView Raw
1## Analytics Reporter
2
3*This project is still under construction.*
4
5A lightweight system for publishing analytics data from Google Analytics profiles.
6
7Available reports are named and described in [`reports.json`](reports.json). For now, they're hardcoded into the repository.
8
9### Installing
10
11* Install through npm:
12
13```bash
14npm install -g analytics-reporter
15```
16
17* [Create an API service account](https://developers.google.com/accounts/docs/OAuth2ServiceAccount) in the Google developer dashboard.
18
19* Take the generated client email address (ends with `gserviceaccount.com`) and grant it `Read & Analyze` permissions to the Google Analytics profile(s) whose data you wish to publish.
20
21* Download the `.p12` private key file from the dashboard, and transform it into a `.pem` file:
22
23```bash
24openssl pkcs12 -in <name of your p12 key>.p12 -out secret_key.pem -nocerts -nodes
25```
26
27* Set the following environment variables:
28
29```bash
30export ANALYTICS_REPORT_EMAIL="asdfghjkl@developer.gserviceaccount.com"
31export ANALYTICS_REPORT_IDS="ga:XXXXXX"
32export ANALYTICS_KEY_PATH="/path/to/secret_key.pem"
33```
34You may wish to manage these using [`autoenv`](https://github.com/kennethreitz/autoenv).
35
36* Test your configuration by printing a report to STDOUT:
37
38```bash
39./bin/report users
40```
41
42If you see a nicely formatted JSON file, you are all set.
43
44* (Optional) Authorize yourself for S3 publishing.
45
46If you plan to use this project's lightweight S3 publishing system, you'll need to add 6 more environment variables:
47
48```
49export AWS_REGION=us-east-1
50export AWS_ACCESS_KEY_ID=[your-key]
51export AWS_SECRET_ACCESS_KEY=[your-secret-key]
52
53export AWS_BUCKET=[your-bucket]
54export AWS_BUCKET_PATH=[your-path]
55export AWS_CACHE_TIME=0
56```
57
58### Use
59
60Reports are created and published using the `analytics` command.
61
62```bash
63analytics
64```
65
66This will run every report, in sequence, and print out the resulting JSON to STDOUT. There will be two newlines between each report.
67
68A report might look something like this:
69
70```javascript
71{
72 "name": "devices",
73 "query": {
74 "dimensions": [
75 "ga:date",
76 "ga:deviceCategory"
77 ],
78 "metrics": [
79 "ga:sessions"
80 ],
81 "start-date": "90daysAgo",
82 "end-date": "yesterday",
83 "sort": "ga:date"
84 },
85 "meta": {
86 "name": "Devices",
87 "description": "Weekly desktop/mobile/tablet visits by day for all .gov sites tracked by the U.S. federal government's Digital Analytics Program."
88 },
89 "data": [
90 {
91 "date": "2014-10-14",
92 "device": "desktop",
93 "visits": "11495462"
94 },
95 {
96 "date": "2014-10-14",
97 "device": "mobile",
98 "visits": "2499586"
99 },
100 {
101 "date": "2014-10-14",
102 "device": "tablet",
103 "visits": "976396"
104 },
105 // ...
106 ],
107 "totals": {
108 "devices": {
109 "mobile": 213920363,
110 "desktop": 755511646,
111 "tablet": 81874189
112 },
113 "start_date": "2014-10-14",
114 "end_date": "2015-01-11"
115 }
116}
117```
118
119#### Options
120
121* `--output` - Output to a directory.
122
123```bash
124analytics --output /path/to/data
125```
126
127* `--publish` - Publish to an S3 bucket. Requires AWS environment variables set as described above.
128
129```bash
130analytics --publish
131```
132
133* `--only` - only run one report.
134
135```bash
136analytics --only devices
137```
138
139* `--slim` -Where supported, use totals only (omit the `data` array). Only applies to JSON, and reports where `"slim": true`.
140
141```bash
142analytics --only devices --slim
143```
144
145* `--csv` - Gives you CSV instead of JSON.
146
147```bash
148analytics --csv
149```
150
151* `--frequency` - Limit to reports with this 'frequency' value.
152
153```bash
154analytics --frequency=realtime
155```
156
157* `--debug` - Print debug details on STDOUT.
158
159```bash
160analytics --publish --debug
161```
162
163### Public domain
164
165This project is in the worldwide [public domain](LICENSE.md). As stated in [CONTRIBUTING](CONTRIBUTING.md):
166
167> This project is in the public domain within the United States, and copyright and related rights in the work worldwide are waived through the [CC0 1.0 Universal public domain dedication](https://creativecommons.org/publicdomain/zero/1.0/).
168>
169> All contributions to this project will be released under the CC0 dedication. By submitting a pull request, you are agreeing to comply with this waiver of copyright interest.