Skip to content

Commit

Permalink
manually send country/region ip info with this add to the uw trojan
Browse files Browse the repository at this point in the history
  • Loading branch information
julesghub committed Jun 7, 2024
1 parent e62a9e3 commit 9eb7c96
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/underworld/_net/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@
API_SECRET = "cC7EhGFMQZaWgVsGUT5Zow"
GA_CLIENT_ID = uw._id


# Function to get user's IP address and geographic information
def get_geo_info():
url = 'https://ipinfo.io/json'
# try because connection to ipinfo ping may fail
try:
with urllib.request.urlopen(url) as response:
if response.status == 200:
data = response.json()
return {
"country": data.get("country"),
"region": data.get("region"),
"city": data.get("city")
}

except:
pass
return None

def PostGA4Event( event_name, ev_params ):
"""
Posts an Event Tracking message to Google Analytics.
Expand Down Expand Up @@ -63,10 +82,10 @@ def PostGA4Event( event_name, ev_params ):
"""
geo_info = get_geo_info()

try:
url = "https://www.google-analytics.com/"
url += f"mp/collect?measurement_id={GA4_TRACKING_ID}&api_secret={API_SECRET}"
url = f"https://www.google-analytics.com/mp/collect?measurement_id={GA4_TRACKING_ID}&api_secret={API_SECRET}"

payload = {
"client_id": GA_CLIENT_ID,
Expand All @@ -76,13 +95,13 @@ def PostGA4Event( event_name, ev_params ):
"params": {
"session_id": GA_CLIENT_ID,
"engagement_time_msec": 1,

}
},
}
]
}

# add the input dict to the event params
payload["events"][0]["params"].update(geo_info)
payload["events"][0]["params"].update(ev_params)

# convert data to btye string
Expand Down

0 comments on commit 9eb7c96

Please sign in to comment.