|
| 1 | +# garf for Google Analytics Data API |
| 2 | + |
| 3 | +[](https://pypi.org/project/garf-google-analytics) |
| 4 | +[](https://pypi.org/project/garf-google-analytics/) |
| 5 | + |
| 6 | +Interacts with [Google Analytics Data API](https://developers.google.com/analytics/devguides/reporting/data/v1/rest). |
| 7 | + |
| 8 | +## Install |
| 9 | + |
| 10 | +Install `garf-google-analytics` library |
| 11 | + |
| 12 | +/// tab | pip |
| 13 | +``` |
| 14 | +pip install garf-executors garf-google-analytics |
| 15 | +``` |
| 16 | +/// |
| 17 | + |
| 18 | +/// tab | uv |
| 19 | +``` |
| 20 | +uv add garf-executors garf-google-analytics |
| 21 | +``` |
| 22 | +/// |
| 23 | + |
| 24 | +## Usage |
| 25 | + |
| 26 | +### Prerequisites |
| 27 | + |
| 28 | +* [Google Analytics API](https://console.cloud.google.com/apis/library/analytics.googleapis.com) enabled. |
| 29 | + |
| 30 | + |
| 31 | +/// tab | cli |
| 32 | +```bash |
| 33 | +echo " |
| 34 | +SELECT |
| 35 | + dimension.country AS country, |
| 36 | + metric.activeUsers AS active_users |
| 37 | +FROM resource |
| 38 | +WHERE |
| 39 | + start_date >= '2025-09-01' |
| 40 | + AND end_date <= '2025-09-07' |
| 41 | +" > query.sql |
| 42 | +garf query.sql --source google-analytics \ |
| 43 | + --output csv \ |
| 44 | + --source.property-id=GA_PROPERTY_ID |
| 45 | +``` |
| 46 | +/// |
| 47 | + |
| 48 | +/// tab | python |
| 49 | + |
| 50 | +```python |
| 51 | +from garf_io import writer |
| 52 | +from garf_google_analytics import GoogleAnalyticsApiReportFetcher |
| 53 | + |
| 54 | +query = """ |
| 55 | +SELECT |
| 56 | + dimension.country AS country, |
| 57 | + metric.activeUsers AS active_users |
| 58 | +FROM resource |
| 59 | +WHERE |
| 60 | + start_date >= '2025-09-01' |
| 61 | + AND end_date <= '2025-09-07' |
| 62 | +""" |
| 63 | + |
| 64 | +fetched_report = ( |
| 65 | + GoogleAnalyticsApiReportFetcher() |
| 66 | + .fetch(query, property_id='PROPERTY_ID') |
| 67 | +) |
| 68 | + |
| 69 | +csv_writer = writer.create_writer('csv') |
| 70 | +csv_writer.write(fetched_report, 'query') |
| 71 | +``` |
| 72 | +/// |
0 commit comments