Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dt 1927 Adding GA4 server side #199

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<hr>`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.

Expand Down
36 changes: 33 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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'
Expand All @@ -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
Expand Down Expand Up @@ -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'):
Expand Down
Loading