How to access page path and URL parameters inside a custom web pixel script.
Constraint: In the web pixel sandbox, direct access to window.location is blocked, so typical browser globals aren’t available.
Working approach: Read the URL from the event object’s safe context (event.context.document.location.href) inside analytics.subscribe for your custom event. Then parse it using the standard URL API to extract pathname (page path) and searchParams (URL parameters).
Example outcome: From the full href you can derive the path like /products/shirt and query values such as variant=123.
Conflicting advice: Another reply suggested using window.location.pathname and window.location.search directly. This contradicts the sandbox limitation and may not work in web pixel scripts.
Resolution/status: The context-based method (via event.context.document.location.href + URL parsing) was confirmed to work by the original poster. Discussion effectively resolved with a concrete, viable solution.
Summarized with AI on December 11.
AI used: gpt-5.
I’m trying to either get the page path or the URL parameters for a custom event, but I can’t find anything in the web pixels API documentation. Is there any way to access that information with a custom web pixel script?
Yes, you can get both the page path and URL parameters in a custom web pixel script using plain JavaScript. For example:
// Get the page path
const pagePath = window.location.pathname;
// Get the full URL parameters
const urlParams = window.location.search;
// Example: log them or send them with your custom event
console.log(‘Page Path:’, pagePath);
console.log(‘URL Params:’, urlParams);
window.location.pathname gives you the part after your domain (like /products/item1) and window.location.search gives you the query string (like ?rf=123). You can include these in your custom event payload to track them.