Use this script to easily back up and export your legacy data from Google Analytics (Universal Analytics) into a single CSV file per views, using the Google Analytics API.
You'll have a local copy of your valuable historical Google Analytics data, that can be easily loaded into any data visualization tool.
- Visit Google Cloud Console.
- Create a new project. Give it a simple name like "Universal Analytics Export".
- Navigate to the 'APIs & Services' dashboard. Enable both the "Google Analytics API" and "Analytics Reporting API".
- In your GCP project, head over to 'Credentials'.
- Click on 'Create credentials'. Choose 'OAuth client ID'.
- Configure the consent screen. You'll need to set up an OAuth consent screen. It's a bit like introducing your app to Google. Make sure to add your email address as a test user (of the Google account you're using to access Analytics).
- Set the application type to 'Desktop app'.
- Name your OAuth client ID and click 'Create'.
- Take note of the generated Client ID and Client Secret. You'll need these next.
- Clone this repository to your local machine.
- Open the script and look for the section where you can enter your credentials. It'll be near the top (search for "new ClientSecrets").
- Replace the placeholders with your Client ID and Client Secret.
- Execute the script. If it's your first run, Google will ask you to authorize your app.
- Sit back and watch the magic happen. The script will fetch the data for each view, and save it to a CSV file in the same folder as the executable.
The script was designed to retrieve the data that 95% of users are likely interested to preserve, but if needed, you can easily customize the dimensions and metrics.
-
Explore Available Options:
- Visit the Google Analytics Query Explorer.
- Here, you can experiment with different dimensions and metrics to see what data is available for extraction.
-
Select Your Dimensions and Metrics:
- Identify the dimensions and metrics that are most relevant to your needs.
- Make a note of their exact names as they appear in the Query Explorer (e.g.,
ga:sessions,ga:pageviews).
-
Update the Script:
- Open the script in your preferred text editor.
- Locate the section where dimensions and metrics are defined for the
ReportRequestobject. - Replace or add the dimensions and metrics in the script with those you've selected.
- For example:
Dimensions = new List<Dimension> { new() { Name = "ga:date" }, new() { Name = "ga:sourceMedium" }, // Add or replace dimensions here }, Metrics = new List<Metric> { new() { Expression = "ga:users" }, new() { Expression = "ga:newUsers" }, // Add or replace metrics here }
-
Run the Script with Custom Dimensions/Metrics:
- Save the changes to the script.
- Run the script as usual. The data exported will now reflect the custom dimensions and metrics you've specified.
- Balance the Data: Be mindful of the volume of data you're requesting. More dimensions and metrics can lead to larger datasets, which might take longer to process.
- Combinations: Some dimensions and metrics can only be queried together in certain combinations. The Query Explorer is a great tool to validate these combinations before updating the script.
- API Limits: Keep in mind the API limits. Excessive data requests might lead to hitting the quota limits.
By customizing dimensions and metrics, you can fine-tune the data extraction to align with your analysis requirements, making the script a powerful tool for your specific data needs.
Found a bug? Got an idea for an improvement? Feel like adding a cherry on top? Your contributions are welcome! Fork it, branch it, push it, and make a pull request. Let's make Google Analytics data archival a piece of cake for everyone! 🍰
With the transition from Universal Analytics to Google Analytics 4 (GA4), Google horribly decided not to migrate existing user data to GA4, nor to keep a way to consult it online in a legacy way, nor to at least offer a simple way to download the raw data so it could be loaded into another program.
I tried all the alternative solutions I could find to archive this data, such as the Google Analytics and SyncWith add-ons for Google Sheets, Airbyte through their cloud service, Restack.io (to export the data to BigQuery), with Docker locally, Supermetrics and Dataddo. Unfortunately, each time it was either too unreliable, cumbersome, slow, or it didn't work at all. The Google Analytics add-on for Google Sheets for example is currently impossible to install as Google recently started blocking it for security reasons...
So I ended up writing this script with ChatGPT's help, which does the job neatly and quickly, without running into any API limitation issues.
This script is released under the MIT License. Feel free to use it for your personal or commercial needs, and share it with your friends.