Optimize for non-Webflow sites

Tag page types with global variables

Updated

Use global window variables to clarify page identity when URLs overlap.

If multiple page types share the same URL pattern, you can add global variables to your page’s JavaScript to help Optimize distinguish between them. This ensures your page definitions only target the right pages — without relying on full page load or brittle workarounds.

Why use global variables for page type

When different pages on your site (e.g., search form and search results) have identical or overlapping URL structures, it can be tricky to isolate one for optimization. Setting a global variable lets you differentiate them clearly and quickly — even before the page finishes loading.

Set a global window variable

In the page’s JavaScript, define a global variable using the window object. Name the variable to reflect the page type, and assign a distinct string value for each type. For example:

// On the search page
window.PageType = 'coffeeSearchPage';

// On the search results page
window.PageType = 'coffeeSearchResultsPage';

Important

Set the global variable before the Optimize snippet runs so it can be read immediately.

Use the variable in page definitions

Once the variable is in place, you can use the Page code section in your page definition settings to check for its presence. This ensures Optimize applies the definition only to pages where the variable matches your expected value.

For example, to include only search result pages, you might add the following to your page code logic:

return window.PageType === 'coffeeSearchResultsPage';