The repository is a sample on how to implement Google Analytics 4 and Google Universal Analytics in static web site.
The main idea of Google Analytics 4 is to generate a report with page views in Python Google Analytics library on schedule and store it in a javascript file as a variable.
Google Universal Analytics was retired on Jul 1, 2024. If you was able to export your report with page URLs and counters, you can add it to your Web site. The exported data should be converted into Map constant.
The sample content is.
-
show_pageviews.html - shows pageviews counter.
-
pageviews.js - retrieve stored Google Analytics 4 and Universal Analytics reports from javascript files.
-
blog_metrics.js - Google Analytics 4 report data.
-
blog_metrics_universal_analytics.js - Google Universal Analytics report data.
-
generate_page_views_analytics4.py - a tool to generate Google Analytics 4 report data on schedule.
-
[key file].json - service account key JSON file for Google Analytics 4.
usage: generate_page_views_analytics4.py [-h] -p PROPERTY_ID -o OUTPUT_PATH
Generate Google Analytics 4 page view report file v. 1.0.0
optional arguments:
-h, --help show this help message and exit
-p PROPERTY_ID, --propertyid PROPERTY_ID
Google Analytics 4 property_id
-o OUTPUT_PATH, --outputpath OUTPUT_PATH
Google Analytics 4 page views report file output path
Sample of usage in Alpine Linux where the tool and service account file located in /opt/generate_page_views_analytics4
folder and web site in /var/www/sample.com
folder.
export GOOGLE_APPLICATION_CREDENTIALS="/opt/generate_page_views_analytics4/tech-jogging-blog-58s414ae632b.json"
python3 /opt/generate_page_views_analytics4/generate_page_views_analytics4.py -p 123456789 -o /var/www/sample.com/blog_metrics.js
-
Set up Google Analytics for using Google Analytics 4 Data API.
-
Install Python library
pip install google-analytics-data
- Publish files to a web server.
- show_pageviews.html
- pageviews.js
- blog_metrics.js
- Open show_pageviews.html page.
-
Make changes to your Web site pages as per samples in files.
- pageviews.js
- show_pageviews.html
-
Publish files to a Web server folder.
- Modified pageviews.js
- Your updated Web pages.
-
Copy files to your Linux machine.
- [key file].json
- generate_page_views_analytics4.py
-
Run
generate_page_views_analytics4.py
file to generateblog_metrics.js
page views report file. -
Copy
blog_metrics.js
file to Web server folder. -
Open your web site pages.
-
Switch to root account if needed.
sudo su
-
Open
crontab
editor.crontab -e
-
Add the command item.
It runs cron job once an hour. Based on my experience, Google Analytics 4 database is updated daily.
0 * * * * python3 /opt/generate_page_views_analytics4/launcher.sh
launcher.sh
is located in/opt/generate_page_views_analytics4
folder and encapsulate the logic to run the process.export GOOGLE_APPLICATION_CREDENTIALS="/opt/generate_page_views_analytics4/[key file].json" python3 /opt/generate_page_views_analytics4/generate_page_views_analytics4.py -p [property_id] -o /var/www/sample.com/blog_metrics.js
-
Validate your setup.
crontab -l