Goodbye, Google Analytics - Why and How You Should Leave The Platform
Tags: analytics, googlecloud, python, datascience
Published: 2022-04-19 18:06:57


With the recent events relating to _Google Analytics_ platform, it's becoming very clear that the time has come for many of us to migrate from Google Analytics to different platforms.

In this article we will go over both the _"Why?"_, so that you can make an informed decision whether you need to migrate of not, as well as the _"How?"_ of migrating from Google Analytics - that is quickly and easily taking your data and moving to different analytics platform without too much hassle.

## Why?

Why has now - all of a sudden - came time to ditch Google Analytics?

There are 2 main reasons why you might want to do so. First being that there have been court rulings in multiple EU countries stating that it's illegal to use Google Analytics in EU. This is because of data of EU citizens is being transferred to US, which violates GDPR rules. These ruling apply so far to [France](https://www.cnil.fr/en/use-google-analytics-and-data-transfers-united-states-cnil-orders-website-manageroperator-comply) and [Austria](https://noyb.eu/en/austrian-dsb-eu-us-data-transfers-google-analytics-illegal), but more are expected in 2022. So, unless Google decides to run GA infrastructure in EU pretty soon, then you might be left in legal hot water pretty soon.

Recently, US and EU leadership reached [agreement on trans-Atlantic data privacy](https://www.nytimes.com/2022/03/25/business/us-europe-data-privacy.html), which might alleviate this issue, but chances are that data privacy advocates won't be satisfied and will challenge this in courts.

The other reason to migrate off the platform is that [Google is deprecating _Universal Analytics_](https://blog.google/products/marketingplatform/analytics/prepare-for-future-with-google-analytics-4/) beginning 1 July 2023. You could migrate your GA properties to GA4 as advised by Google, the problem here's though that your old data won't move and you won't be able to compare or look back at analytics from let's say 2 years ago, which sucks.

The solution that _"kills two birds with one stone"_ is to choose a privacy-friendly platform where you won't need to worry about GDPR rules and extract and move your data from GA to said platform, taking full ownership of your data and not having to worry about Google deprecating things whenever they please (which is [very often](https://killedbygoogle.com/)).

## How?

If you decided that you want to or need to migrate, then here I present you a simple tool and process for exporting your analytics data and getting it into a format suitable for other platforms.

[_GA Extractor_](https://github.com/MartinHeinz/ga-extractor) can help you extract any analytics data from Google Analytics without having to touch the API. Apart from performing basic data exports, it can also transform it into other formats besides default JSON - that being - a more readable CSV, which can be used in data analysis or SQL suitable for direct migration to other platforms. 

Let's look at some examples. To install the tool just run:

```bash
pip install ga-extractor
ga-extractor --help
# Usage: ga-extractor [OPTIONS] COMMAND [ARGS]...
# ...
```

From there you can set it up like so:

```bash
ga-extractor setup \
  --sa-key-path="analytics-api-24102021-4edf0b7270c0.json" \
  --table-id="123456789" \
  --metrics="ga:sessions" \
  --dimensions="ga:browser" \
  --start-date="2022-03-15" \
  --end-date="2022-03-19"
```

In the command above, you can see that you need to provide _Service Account key_ file (`--sa-key-path`) and _Table ID_ (`--table-id`, also referred to as _View ID_). You get these values using the guide in [repository README](https://github.com/MartinHeinz/ga-extractor#readme) or on [PyPI page](https://pypi.org/project/ga-extractor/0.1.1/).

Running this command will generate a config file in an application config directory in `~/.config/ga-extractor/...`. You can verify that the tool can authenticate with Google API by running `ga-extractor auth`.

If you're unsure as to which metrics and dimensions to select, then you can use `--preset` flag with `FULL` or `BASIC` option to set the metrics and dimensions for you.

With tool configured, you can now run extraction:

```bash
ga-extractor extract --report="my-awesome-report.json"
# Report written to /home/user/.config/ga-extractor/my-awesome-report.json

cat /home/user/.config/ga-extractor/my-awesome-report.json | jq .
# ...
```

Now, in case the data format produced by Google API isn't suitable for your needs, then you can use `migrate` command to extract and transform it into CSV or SQL: 

```bash
ga-extractor migrate --format=CSV
# Report written to /home/user/.config/ga-extractor/02c2db1a-1ff0-47af-bad3-9c8bc51c1d13_extract.csv

head /home/user/.config/ga-extractor/02c2db1a-1ff0-47af-bad3-9c8bc51c1d13_extract.csv
# path,browser,os,device,screen,language,country,referral_path,count,date
# /,Chrome,Android,mobile,1370x1370,zh-cn,China,(direct),1,2022-03-18
# /,Chrome,Android,mobile,340x620,en-gb,United Kingdom,t.co/,1,2022-03-18

ga-extractor migrate --format=UMAMI
# Report written to /home/user/.config/ga-extractor/cee9e1d0-3b87-4052-a295-1b7224c5ba78_extract.sql
```

The CSV is much more readable than the default output, while the SQL can be used to migration to other platforms. The SQL here is tailored for _Umami_ Analytics, but with some tweaks it could be applied to other platforms that track sessions and page views along with all the common metrics.

Be aware that the `migrate` command overrides previously configured metrics and dimensions, so that it can produce meaningful results.
  
If you don't have a lot of data or you feel like the above is too much hassle for you, then you can also export some tables and views from GA web console. There are a lot of advanced/complex things you can export, but 2 most basic things you surely want take before leaving GA is page views and user sessions information.

To export page view count or really any aggregated metric, you can navigate to _Audience_ and _Overview_, choose desired metric from dropdown and click export:

![User Export](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oyxruqfmujzqrsghyo3p.png)

To export individual sessions you can navigate to _Audience_ and _User Explorer_, and click export:

![Session Export](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0dngxwqvz0ik7a9yir9c.png)

There are many more things you can grab from the console, and I recommend checking out [this article](https://blog.coupler.io/how-to-export-google-analytics-data-guide/) which provides good overview.

## Alternatives

Now that you have the data exported, or you maybe decided to leave GA behind without even taking the data, it's time to choose the new platform. There are many good privacy-friendly, open source, cheap/free GA alternatives. I won't go over every single one of these as there are great articles written about this, like [this one](https://stackdiary.com/open-source-analytics/) with a list of open-source options.

If you exported data using the `ga-exporter` tool presented above you have the option to transform it into format that can be readily inserted into _Umami_ which makes it easy migration target. If you want to test it out, then you can spin up the service with _Docker_ as described in [docs](https://umami.is/docs/install), add your website as shown [here](https://umami.is/docs/add-a-website) and then just insert previously exported data with:

```bash
cat  /home/user/.config/ga-extractor/cee9e1d0-3b87-4052-a295-1b7224c5ba78_extract.sql | psql -Upostgres -a db
```

You can then view the dashboard, which should show you your data like so:

![Umami Dashboard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0f83yl746qzsx38e2bvy.png)

Umami is my migration target, you might however have different preferences and needs. One thing to keep in mind though, when choosing your target platform, is full data _"ownership"_, which is important to avoid vendor lock, which might otherwise make it hard to migrate in the future, if the need arises again. So, I'd recommend to look for a platform that makes it easy to export full analytics data in practical format(s) to make it easy to analyze your data or migrate elsewhere.

## Closing Thoughts

Even if you decide that you don't want to or don't need to leave Google Analytics at this time, it's good idea to explore some of the alternatives. Chances are that you don't need any of the advanced GA features, and you might just realize that the alternatives are actually more suitable for your needs.

There's also no need to be scared of self-hosting the analytics engine yourself. Many of the open-source solutions can be spun up in matter of minutes and require very little resources to run. Even data migration can be quite simple as you've seen earlier in this article. If that's the route you want to go, but the extractor tool presented here doesn't support the target platform you'd like to migrate to, or you have some feedback to share, then feel free to create and an issue in [GitHub repository](https://github.com/MartinHeinz/ga-extractor) and I will definitely try to help out.