JS API docs


PageVitals has a JavaScript API where you can interact with the Field Testing script installed on your page.

You can do things like adding tags, setting custom variables and changing the path of the page if you want to group your pages or use special paths for A/B testing variants.

Interacting with the Field Testing script requires you to first enable Field Testing on your site.

The Field Testing script loads asynchronously so all interaction with the script is done by pushing calls to the $pagevitals object which will then dispatch the call to the script once ready. But first you need to make sure that the $pagevitals object is present. It's done like this:

(window.$pagevitals ||= []).push({ tags: "my-tag" });

The above code initializes the $pagevitals object if it's not available, and pushes the "my-tag" tag to it.

If you have many consecutive calls in your code, your only need to do this once:

(window.$pagevitals ||= []).push({ tags: "my-tag" });
window.$pagevitals.push({ path: "/products/{product-name}" });
window.$pagevitals.push({ variables: { react_hydrate_time: 123 }});

You can even omit the window part if you're sure you're executing the code in the window context.

In the JS API documentation, we will assume that the $pagevitals object is not available.