You can use Google Analytics4 API to automate and extract data using code. If you already have a website and want to get intelligent insights, you may consider setting up the API. You can then make python scripts and dashboards to get easy and useful insights about your data.
Prerequisites :
- Google developer profile :
To get one , sign in to your google account and activate it by going to developers.google.com.
- A google analytics property setup.
This can be a blog or a website whose analytics you want to track. If you want to learn more about how to set up google analytics for your website, click here.
Step1: Enable the api
Check google analytics4 api here.
Click quickstart and write name of project. You may call it anything you like. We called it GATest
Agree to the terms and conditions and click next. You should be given a private key. Download it as json. Remember not to share this key publicly.
Step 2: Add service account to the Google Analytics Property
Go to the secret key downloaded in the step above. In this case, it is named service_account.json. Search for client_email
field to obtain the service account email address that looks similar to:
quickstart@PROJECT-ID.iam.gserviceaccount.com
Sign in to your google analytics 4. Go to the left panel and click on admin
This should open your properties. Choose “Property Access Management” option for your desired property.this should give a list of user and the permissions allotted to them. Click on a blue ‘+’ button on the right
Add the client_email you copied here. Choose the permission you wish to allot. You can choose “editor” or “viewer” in this case.
This should set up your google analytics api for your particular property.
Step 3: Note your property id
You would need your property id in your python script. To note your property id, go to the admin option in your google analytics (Like in the step above). Your property id should be on the top next to the property name. It should be a sequence of numbers written in brackets like (123456789).
Now, you are all set to write scripts to automate and extract data about your website. To learn how to write a python script to get 5 most popular pages of your website click here.