Track page views

Page views are events representing when people visit pages on your site. When you send page view data, you can segment your audience based on pages they have, or have not, visited.

Capture page views

To capture page view events, add our JavaScript snippet to your site. We track page views automatically when a person loads any page containing the snippet. On page load, the snippet sends an initial event, where the event name of a page view event is the URL of the page that a person visited.

The JavaScript snippet also turns URL parameters into event data, helping you capture additional information about page views—a user’s search terms, products that a person has filtered for, dates that a user is interested in, etc.

Sending your own page views with the JavaScript snippet

You can use the JavaScript snippet to send page events manually. You might do this if have a single page application, or want to send manual page view events after your page loads.

For example, if you want to take advantage of page rules to send in-app messages in your single page application, you’ll need to send page events so we know what “page” you want your message to appear on.

The first parameter in the request is the page name or URL. If you leave this value empty, we use window.location.href (the entire page URL). You can also pass an object containing additional event data.

<script type="text/javascript">
    _cio.page("pageName", {"extraDataObject": "moreData"});
</script>

Send page views through the API

You can also send page view events through our API. If your account is based in the EU, make sure you send requests to track-eu.customer.io.

curl -i https://track.customer.io/api/v1/customers/5/events \
-u YOUR-SITE-ID-HERE:YOUR-SECRET-API-KEY-HERE \
-d name=http://google.com/search \
-d type=page \
-d data[referrer]=http://google.com

URL parameters in pageview events

The JavaScript snippet captures URL parameters as event data, helping you capture additional information about page views—a user’s search terms, products that a person has filtered for, dates that a user is interested in, etc.

So, for example, a url in the format https://example.com/products?property1=stuff&property2=things. We capture property1 and property2 as event data. The resulting page view event looks like this:

{
    "property1": "stuff",
    "property2": "things"
}

The Events API does not automatically translate URL parameters to event properties. If you use the Events API and you want to segment based on URL parameters. In the example below, we’ve passed a page view event with a URL parameter called search_prop.

curl --request POST \
  --url https://track.customer.io/api/v1/customers/{identifier}/events \
  --header "Authorization: Basic $(echo -n site_id:api_key | base64)" \
  --header 'content-type: application/json' \
  --data '{"type":"page","name":"https://example.com","data":{"search-prop":"example-search"}}'

Create segments based on page views

  1. Go to Segments.

  2. Click Create Segment and create a Data-driven segment.

  3. Set a Page condition with URL matching and then set the URL you want to segment on. You can create segments based on pages users have or have not viewed.

    image.png
    image.png

    You can also click Refine, to segment page views based on a time frame or the number of times a person viewed a page e.g., “has not viewed in 30 days”, or “has viewed at least once”. You can also segment based on URL parameters that you captured by your event. The JavaScript snippet automatically captures URL parameters; if you send events using the Events API you must specify URL parameters in the data object.

    image.png
    image.png

Important things to know:

When defining the URL for a page view:

  • To track a specific page, enter the full path, like this: http://customer.io/docs/pageviews/
  • To track any page, use the asterisk *
  • You can also use * as a wildcard to match anything before or after a URL, just like in the above screenshot. For example, http://customer.io/docs/* will track any page within the docs sub-directory
  • If you use the JavaScript snippet, we’ll automatically capture URL parameters as event data.
Copied to clipboard!
Is this page helpful?