Getting the page path in a customer event

Topic summary

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.

Hi everyone,

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?

Thanks in advance!

Hey pal @hdewendt !

You cannot use window.location because the sandbox blocks it.

Instead, you must access the URL through the event object’s context.

You can use event.context.document.location.href and parse it with the standard URL API:

analytics.subscribe('your_custom_event', (event) => {
  // 1. Get the full URL from the safe context
  const rawUrl = event.context.document.location.href;
  const urlObj = new URL(rawUrl);

  // 2. Get the Path (e.g., "/products/shirt")
  const path = urlObj.pathname;

  // 3. Get Parameters (e.g., "123" from ?variant=123)
  const myParam = urlObj.searchParams.get('variant');

  console.log(`Path: ${path}, Param: ${myParam}`);
});

Hope this helps!

1 Like

That worked, thank you so much!

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.

Hope this helps! :slightly_smiling_face: