diff --git a/README.md b/README.md
index a1f039ba..4050565f 100644
--- a/README.md
+++ b/README.md
@@ -56,9 +56,10 @@ DOCS_GITHUB_REPO_URL=https://github.com/uktrade/public-data-api \
| READ_AND_WRITE_AWS_ACCESS_KEY_ID | The AWS access key ID that has write permissions on the S3 bucket (for the csv-generating worker) |
| READ_AND_WRITE_AWS_SECRET_ACCESS_KEY | The secret part of the read+write AWS access key |
| ENVIRONMENT | The current environment where the application is running
`develop` |
-| GA_ENDPOINT | The endpoint to send analytics info to |
-| GA_TRACKING_ID | The unique identifier for the google analytics property |
-| SENTRY_DSN | The DSN of the Sentry server to report exceptions to |
+| GA_ENDPOINT (deprecated) | The endpoint to send analytics info to |
+| GA_TRACKING_ID (deprecated) | The unique identifier for the google analytics property |
+| GA4_API_SECRET | The API secret for Google Analytics 4 (GA4) |
+| GA4_MEASUREMENT_ID | The measurement ID for Google Analytics 4 (GA4) |
Environment variables used for serving API documentation.
diff --git a/app.py b/app.py
index ba5151f6..f30b5f49 100644
--- a/app.py
+++ b/app.py
@@ -78,6 +78,8 @@ def proxy_app(
endpoint_url,
region_name,
ga_tracking_id,
+ ga4_api_secret,
+ ga4_measurement_id,
):
parsed_endpoint = urllib.parse.urlsplit(endpoint_url)
PoolClass = (
@@ -125,8 +127,8 @@ def stop():
def track_analytics(handler):
"""Decorator to send analytics data to google in the background."""
- def _send(requester_ip, request_url, request_headers):
- logger.info('Sending to Google Analytics %s...', request_url)
+ def _send_to_ua(requester_ip, request_url, request_headers):
+ logger.info('Sending to Google Analytics (UA) %s...', request_url)
requests.post(
os.environ.get(
'GA_ENDPOINT', 'https://www.google-analytics.com/collect'
@@ -145,10 +147,36 @@ def _send(requester_ip, request_url, request_headers):
},
)
+ def _send_to_ga4(request_url, request_headers):
+ logger.info('Sending to Google Analytics 4 (GA4) %s...', request_url)
+ requests.post(
+ 'https://www.google-analytics.com/mp/collect',
+ params={
+ 'api_secret': ga4_api_secret,
+ 'measurement_id': ga4_measurement_id,
+ },
+ json={
+ 'client_id': str(uuid.uuid4()),
+ 'events': [{
+ 'name': 'page_view',
+ 'params': {
+ 'session_id': str(uuid.uuid4()),
+ 'engagement_time_msec': '100',
+ 'page_location': request_url,
+ 'page_title': 'Data API',
+ 'user_agent': request_headers.get('user-agent', ''),
+ 'referrer': request_headers.get('referer', ''),
+ }
+ }],
+ },
+ )
+
@wraps(handler)
def send(*args, **kwargs):
if ga_tracking_id:
- gevent.spawn(_send, request.remote_addr, request.url, request.headers)
+ gevent.spawn(_send_to_ua, request.remote_addr, request.url, request.headers)
+ if ga4_api_secret and ga4_measurement_id:
+ gevent.spawn(_send_to_ga4, request.url, request.headers)
return handler(*args, **kwargs)
return send
@@ -1038,6 +1066,8 @@ def main():
os.environ['AWS_S3_ENDPOINT'],
os.environ['AWS_S3_REGION'],
os.environ.get('GA_TRACKING_ID'),
+ os.environ.get('GA4_API_SECRET'),
+ os.environ.get('GA4_MEASUREMENT_ID'),
)
if os.environ.get('SENTRY_DSN'):