Tracking Pageviews

For the request structure and examples, see the API Reference: API Reference

To report pageviews for pages that do not have any campaigns, use the Pageview endpoint. This is simply an alias for Choose, but it's called without a campaign name. 

Even before you have a full view of which campaigns should be integrated into your application, it's best to integrate a choose call in all page types in your application with the proper page context (see Page Context Types), without referring to any campaigns yet.

Identify the places in your application where you want Dynamic Yield-managed campaigns to run. Any campaign you run has an API selector name that you'll use to get the desired variation.

Now, adapt your call to choose on each page, so that you also pass the relevant campaign names in the selector parameter. It is perfectly valid to call campaigns which have only a single variation at that time, containing a default payload, in preparation for allowing testing and targeting multiple variations in that component later - without further code changes.

Sample requests

The context of this example is a Product Details page (aka PDP) in an imaginary e-commerce website, sugoi-ne.com. The format differs depending on whether you are using a Dynamic Yield site of type web or API. 

curl --request POST  
  --url <https://dy-api.com/v2/serve/user/choose>  
  --header 'content-type: application/json'  
  --header 'DY-API-Key: baadc6ba740a352c9106dc7857a7eb9c'  
  --data '{  
      "user": {  
        "dyid": "-4350463893986789401",  
        "dyid_server": "-4350463893986789401"  
      },  
      "session": { "dy": "ohyr6v42l9zd4bpinnvp7urjjx9lrssw" },  
      "selector": { "names": ["PDP Top Banner", "PDP Recs"] },  
      "context": {  
        "page": {  
          "type": "PRODUCT",  
          "data": ["7383723-010"],  
          "location": "<https://sugoi-ne.com/men-pants/p7383723-010>",  
          "referrer": "<https://sugoi-ne.com/men-pants>",  
          "locale": "en_US"  
        },  
        "device": {  
          "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",  
          "ip": "54.100.200.255"  
        },  
        "pageAttributes": { "customPageAttribute": "someValue" },  
        "options": { "isImplicitPageview": true }  
      }  
}'

Example for an API-type section

curl --request POST  
  --url <https://dy-api.com/v2/serve/user/choose>  
  --header 'content-type: application/json'  
  --header 'DY-API-Key: baadc6ba740a352c9106dc7857a7eb9c'  
  --data '{  
      "user": { "id": "yaexono4ohphania" },  
      "session": { "custom": "iquahngaishe2koh" },  
      "selector": { "names": ["PDP Top Banner", "PDP Recs"] },  
      "context": {  
        "page": {  
          "type": "PRODUCT",  
          "data": ["7383723-010"],  
          "location": "<https://sugoi-ne.com/men-pants/p7383723-010>",  
          "referrer": "<https://sugoi-ne.com/men-pants>",  
          "locale": "en_US"  
        },  
        "device": {  
          "userAgent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",  
          "ip": "54.100.200.255"  
        },  
        "pageAttributes": { "customPageAttribute": "someValue" },  
        "options": { "isImplicitPageview": true }  
      }  
}'

Let's break this down:

  • The selector argument contains the API selector name of two campaigns that are part of every product page in that website: One for the content of the top banner, and the second for the recommendations widget. (In future versions, additional selector types for fetching a subset of campaigns will be available)
    • The context argument is comprised of three parts:
      • page:
        • The type of the page, such as HOMEPAGE or PRODUCT. Some types also mandate passing additional identifiers in the data property. In the case of a product page, it's the product SKU. For some page types, multiple identifiers may be passed, and so this property is always in array notation. See below for the list of context types and appropriate data parameters.
        • The location of the page, and optionally the referrer from which the user got to this page, if known. For the web, this should be a canonical URL. For apps, this is the logical name of the location or screen.
        • The locale is optional and enables recommendation objects to support multi-locale product feeds. See Multi-Language Support.
      • device: This is optional and applies to requests made from client devices, or on their behalf. It's useful for both targeting and data collection based on device attributes:
        • userAgent is applicable for web browser-based clients, whether they call this API directly or through your server-side. The user agent string usually contains not only the browser name and version but also the operating system and brand of the mobile device - all of which can be used for targeting. If this argument is not passed, the gateway checks whether the API call was made with a User-Agent HTTP header, which is the case when a request is done directly from the browser.
        • ip is the IPv4 or IPv6 address of the client device, if available. This allows for targeting based on geolocation and weather. Note: the IP address is never stored on our end, but rather translated immediately to geolocation. 
      • Additional attributes:
        • pageAttributes is an optional object that enables you to pass any key-value pair you want to use for targeting in this campaign. It is an ad-hoc method for quickly targeting based on any attribute known to the caller of the API. All you need to do is pass these attributes and set targeting rules over them in the Dynamic Yield console. Note that this data is not stored, and thus cannot be used for creating audience groups.
        • options is used to control the behavior of this endpoint. Currently, only the isImplicitPageview boolean parameter is supported, and it is set to true by default (Note: For Web API isImplicitPageview is false by default). Meaning, a pageview is to be reported as part of this call. If you call the endpoint outside the rendering of a new page, set this to false.