Skip to content

Commit

Permalink
Merge pull request #518 from OpenUpSA/ga4-file-downloads
Browse files Browse the repository at this point in the history
Add file_download event params
  • Loading branch information
paulmwatson authored Mar 20, 2024
2 parents 1ca78bb + dcc2319 commit 75d438a
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions pmg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from flask_security import current_user
import requests
import json
import os

# Useragents that are bots
BOTS_RE = re.compile("(bot|spider|cloudfront|slurp)", re.I)
Expand Down Expand Up @@ -70,15 +71,26 @@ def track_file_download():
api_secret = app.config.get("GOOGLE_ANALYTICS_API_SECRET")
client_id = request.cookies.get("_ga")

user_agent = request.user_agent.string
path = request.path
last_slash_index = path.rfind("/")
file_dir = path[:last_slash_index]
page_location = f"{request.url_root}{file_dir[1:]}"

url = f"{ga_url}?measurement_id={ga_id}&api_secret={api_secret}"
payload = {
"client_id": client_id,
"non_personalized_ads": "false",
"events": [
{"name": "file_download", "params": {"userAgent": user_agent, "path": path}}
{
"name": "file_download",
"params": {
"file_extension": path.split(".")[-1],
"file_name": path,
"link_url": request.url,
"page_location": page_location,
"page_referrer": request.referrer,
},
}
],
}
requests.post(url, data=json.dumps(payload), verify=True)
Expand Down

0 comments on commit 75d438a

Please sign in to comment.