TUTORIAL: Python API for Google Analytics (GA4)
Вставка
- Опубліковано 12 лип 2024
- Get your Google Analytics (GA4) data in python through Google Analytics Data API v1 beta.
Create API keys, authorize your app in Google Analytics, create a request and format the response, visualize the data in python or export the data to CSV or Excel. Complementary report code included.
Quickstart Guide:
developers.google.com/analyti...
Python code in GitHub repository:
github.com/tanyazyabkina/GA4_...
Google Developers Guide:
googleapis.dev/python/analyti...
Metrics and Dimensions:
developers.google.com/analyti...
Chapters:
0:00 GA4 to python overview
0:53 API keys using Quickstart
2:51 API keys manually
6:27 Authorize in GA4
8:57 Python setup
10:09 Google analytics data request and response
12:46 Export response in Excel/CSV
13:19 Monthly web traffic chart
13:45 Python GA4 report
15:33 Validate GA4 in python against Data Studio
My website: zyabkina.com
If you want to contact me, the best way is to ping me on LinkedIn using Connect-Add a note (three dots in the mobile app) and mention that you watched my video. - Навчання та стиль
Many, many thanks. I searched a lot for some API tutorial with Python to connect to GA4 that worked. Congratulations, yours is very clear, simple and functional.
You are a LIFESAVER. My least favorite part of analytics is setting up new APIs, but this worked brilliantly, and your code is much cleaner than the quickstart code. Thank YOU!!!!
Thankyou for the video, Tanya!
I just a need a working code to get started with, and what you have provided in the video is more than enough.
Great !! Everything is so simple with your explanations ! Thanks a lot
Great. Thank you for sharing, much appreciated. It solved my problem.
Absolutely a good video for beginners, even though the quality of the video itself only got 480p, still pretty nice! thx!
Thanks for pointing out the resolution. After 15 mins of checking all the versions and settings, I am still confused why this happened and how come I never noticed. Clearly, I am not a good video editor!🙃
love this video. Thank you so much!
Mam you are a lifesaver. I went through so many tutorials but none was as clear and detailed as yours. Thank you for posting this.
Keep up the good work 😀!
Fing awesome mami! TY so much!
Spasibo! 🙏
i like your content
Thank you sooo much for the detailed explanation! I have a question: do you know/have a way to query custom dimensions too? I'm struggling with this right now.
I have not done it using the API, but Google has a way to pull custom dimensions:
developers.google.com/analytics/devguides/reporting/data/v1/api-schema#custom_dimensions
thanks
Very nice video. Question for you. By doing this, could you extract the same day data by hour ? Or do you still need to wait until next next to see today's data?
There is a realtime report: developers.google.com/analytics/devguides/reporting/data/v1/realtime-basics
Hi I have a question. So I have experienced missing data (downloaded the output in CSV) when I added more dimensions to RunReportRequest function. Is there any possible solutions to solve this?
Great Video! Do you have any idea on how to pull custom segment level data as well?
developers.google.com/analytics/devguides/reporting/data/v1/exploration-api-schema
I believe that's the dimension called 'segment': A segment is a subset of your Analytics data. For example, of your entire set of users, one segment might be users from a particular country or city. To learn more, see Segment builder. The segment dimension returns the name assigned to the segment in the request with a 1-based prefix (i.e. if the first segment is named 'Paid Traffic' in the request, then this segment will be '1. Paid Traffic' in the response). If segments names are not specified in the request, segments are named by 1-based names (i.e. '1. Segment', '2. Segment', etc.)
Thank you for the video. Really help to code . I have question: As GA4 api only retrieve the data of 10,000 rows. How can we retrieve all the data (10,000 + rows)?
I have the same problem, could you resolve it?
There are limit and offset options for the number of rows that allow the request to return more than 10K records. Here is a guide that explains how they work:
developers.google.com/analytics/devguides/reporting/data/v1/basics#navigate_long_reports
Great video! I tried this on my local environment and it worked amazingly well, however, how can I automate this to run everyday on cloud using Azure Function App or similar? Would really appreciate some help. Thank you!
I believe, it should be possible. I am not an Azure expert, though.
Very good video. Congratulations
Query: How much is the cost of the API service in GCP?
There is no cost if you stay within Standard Property limits, which are quite generous. Here is more detail on specific quotas: developers.google.com/analytics/devguides/reporting/data/v1/quotas
Do you know if the access token and refresh token are the same from UA to GA4?
If you authorize both UA and GA4 APIs for the project, you should be able to use the same tokens for both.
Hi can we also extract GA4 data only with API. In this tutorial you have used python but I just want to extract with API only
I believe so: developers.google.com/analytics/devguides/reporting/data/v1/rest/v1beta/properties/runReport
Hi! I have a question. Why is the metric "Average Engagement Time" not on the list of metrics for the API, i can see it in "Aquisition->User Aquisition" in the table but its not on the list. Does anyone know how to get it?
There are two metrics that you can use here: userEngagementDuration and averageSessionDuration. Not that the first will give you the total and second the average.
hi !
thanks for this amazing tutorial.
I am getting a SSL Handshake error when trying to run format_report function .
Any idea how it can be resolved ?
I am not sure. I would check property ID and correct credentials location.
I have same problem, did you manage to fix it?
I love your videos. Thanks for share your knowledge. Maybe you are able to help me with a doubt I have an implementation in UA with severals advertisers_id with view_id so I need to migrate all the advertisers to GA4. But I don't have property_id. Are there a way to get the property_id to all my advertisers? What are the steps that I will follow is I only have several advertisers_id?
Describe what you mean "migrate all the advertisers to GA4"? Is this a python task or a UA to GA4 migration task?
@@probabilistically Is a python task, I have a several Google accounts with refresh and access token and I need to migrate all of them to GA4 but I got the 403 User does not have sufficient permissions for this property. Could you give an advice?
@@angelomartinez113 Refresh and access tokens - are you authorizing an app or a service account? If you have a service account, then the "email" (looks like an email, but is not an actual email) needs to be added as a user to the GA4 properties - which are different than the UA properties.
Thank for the fot video !!
I am stuck with this: client = BetaAnalyticsDataClient()
Getting JSONDecodeError & DefaultCredentialsError &
Do you solved it ?
Hey , I have a doubt , is there is any way that I can ask view access in google analytics with the help of python or js script
If yes can you pls tell me the approach?
Thanks in advance
Check out the Analytics Management API. This API allows adding new users into accounts. developers.google.com/analytics/devguides/config/mgmt/v3/mgmtReference
Is it possible to access the conversion data from the library or rest api?
There is a metric called isConversionEvent (boolean), and it marks conversion events that you set up in GA4 Admin -> Events. The same field is availabe in Looker Studio, so if you want to expore it first, I would recommend trying it there.
More here: developers.google.com/analytics/devguides/reporting/data/v1/api-schema
Is there an Id (like userid) to call as dimension to fetch all visitor data ??
The API is good for summaries not all data. Look into BigQuery export for detailed raw data export.
Thanks! @@probabilistically
DefaultCredentialsError: Fil was not found.
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings
What is the correct way to write the Credentials , cuz i get an error in that file , some one can help me ? just an example of how i have to put the credecials
The JSON credentials file downloaded from Google Cloud is all you need. Make sure your file location is referenced correctly.
Hi ma'am, thanks for sharing this video on YT, could you please share a NEW video using the same GA4, Analytic Data API, using .React JS and report can show into the desired URL /admin website?
Unfortunately, am in analytics, so I don't know React JS. Excellent suggestion for someone else, though. @jiejenn, maybe?
I don't understand, you searched for the active users, right? But active users where? How do I search the amount of people in a certain field?
Active users are the count distinct of users/pseudousers from pageview event records. If you limit pageviews to a certain area of the site, you will get users for that area. If you break users up by page, then the sum should be larger than the total for the site because some users go to more than one page.
Hi,
Can we also extract the GA3 data with same method
Yes, you absolutely can. Here is the guide: ua-cam.com/video/N2bbJQroGB4/v-deo.html
Hi @@probabilistically Thanks, seriously your GA4 extract video helped me alot ❤
more questions:-
1) I want to fetch below Dimension/Metrics but not able to see these on API Documentation. Can you help me how I can fetch these
- Goal Completions
- Ad Distribution Network
- Event Category/Action/Label
2) Can GA3 and GA4 data be stitched together? if yes than how we can do?
@@juanitamiles4328 Glad it helped!
@@probabilistically
do you have answer of my below questions ?
1) I want to fetch below Dimension/Metrics but not able to see these on API Documentation. Can you help me how I can fetch these
- Goal Completions
- Ad Distribution Network
- Event Category/Action/Label
2) Can GA3 and GA4 data be stitched together? if yes than how we can do?