kapa.ai how would I write an event function to extract a parameter value from a URL?
Please stand by Matthew M. as I search the knowledge base. I normally take about 15 seconds or so. 👋
Feel free to ask a follow-up question once I've answered by mentioning me with @kapa.ai
You can do this with a JQL map transformation by parsing the URL string and returning the parameter you care about.Here’s a simple JQL example that extracts a utm_source parameter from an event property called current_url: js function main() { return Events({ from_date: params.start_date, to_date: params.end_date }) .map(function(event) { var url = event.properties.current_url || ""; var match = url.match(/[?&]utm_source=([^&#]+)/); return { event: event.name, distinct_id: event.distinct_id, utm_source: match ? decodeURIComponent(match[1]) : null }; }); }LBKSYou’d pass start_date and end_date via params when calling the JQL API, as shown in the JQL parameters example.[JQL params]
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
kapa.ai it looks like JQL is no longer supported, is there another method?
Let me check the knowledge base for you again. One moment! 📚
You’re right that JQL is in maintenance mode and Mixpanel recommends using other methods instead.[JQL app][Custom JQL]For extracting a parameter from a URL going forward, the recommended approaches are: 1. Do it at tracking time (preferred)
Parse the URL in your application and send the parameter as an event property. For example, on web, Mixpanel’s JS SDK already auto-captures UTM parameters and click IDs from the page URL and attaches them to events, so you don’t need a separate export-time function for those.[JS UTM tracking]
On server-side, you can similarly parse query parameters from the request URL and add them to properties before calling track().[Server-side attribution]2. Export raw events and parse URLs outside Mixpanel
If you already have events with a URL property and now want to extract a parameter, export the raw events and do the parsing in your own code or warehouse:
- Use the Raw Event Export API to pull events (optionally filtered by event name or where clause).[Export API; Export methods]
- Example export request:[Project migration export]
--url '[https://data.mixpanel.com/api/2.0/export?from_date=2024-07-23&to_date=2024-07-24&event=sample_event](https://data.mixpanel.com/api/2.0/export?from_date=2024-07-23&to_date=2024-07-24&event=sample_event)' \
--header 'accept: text/plain' \
--header 'authorization: Basic YOUR_TOKEN'
```LBKS - The response is JSONL (one event per line); you can then parse the URL field and extract query parameters in Python, SQL, etc.LBKSThere isn’t a newer Mixpanel-side “event function” API (other than JQL) documented in the sources for doing arbitrary string parsing at query time, so the supported pattern is: extract at ingestion, or export and transform externally.
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
kapa.ai this isn't what I'm after I'm afraid, Can I do with it a Regex Extract?
Let me check the knowledge base for you again. One moment! 📚
Yes, you can do this with a Custom Property using regex_extract on the URL property.1. Go to Lexicon → Custom Properties → New Custom Property.
2. Use a formula like: text regex_extract(properties["page_url"], "[?&]utm_source=([^&#]+)", 1)LBKSThis: - Looks in properties["page_url"] (replace with your URL property name).
- Finds utm_source=... in the query string.
- Returns just the value (capture group 1).[Custom property functions]You can adapt the regex for any parameter, e.g. my_param: text regex_extract(properties["page_url"], "[?&]my_param=([^&#]+)", 1)LBKS
kapa.ai is still learning and improving, please let me know how I did by reacting below.
ℹ️ Tip: Mention @kapa.ai in the thread for follow-ups or DM the app for 1-to-1 help.
