Option 2: Auto-detect page context

This method enables you to be flexible and make changes without altering your site's code. Note, however, that the auto-detection might cause a very small delay in serving campaigns (also known as "flicker"). Note also that this out-of-the-box feature might still require some code adjustment.

  1. Determine how you want to define each page type. Currently, this is done using a pattern in the page URL ('contains' or 'regex'), or using the page tag. Note: In the URL option, parameters are ignored for evaluation purposes.
  2. For all page types except Homepage, you also need to define the page's attributes to complete the context data. This can be done in the page URL, or by inputting the relevant data attribute.
  3. To check that the rules are defined properly:
    1. Click Verify, and then select the page selecting the page
    2. Review how Dynamic Yield identifies this page using our Implementation Helper.
  4. If your site is multi-language, add a slimmer version of the context to your site code, with the "lng" attribute only. For example:
    DY.recommendationContext = {lng: 'en_GB'};

If you make any changes to the site in the future, make sure you update the relevant context settings as well.

Regex examples

The following are examples of regular expressions for context on each of these page types:

Homepage

The regular expression (regex) added in this context rule is a string added to the end of the homepage URL: www.mysite.com/[REGEX], where the tag [REGEX] is replaced with whatever expression you want to use. Note that there can be no additional text after the expression.

Example:

Expression: /\w{2}/\w{2}/$ (URL is www.mysite.pl/\w{2}\/\w{2}\/$])

This rule finds this URL: www.mysite.com/pl/pl

This rule does not find this URL: www.mysite.com/pl/pl/new-in/men

Product page

To set a regex for context on a product page, there are two parts: Define the page type (set product targeting) and define the SKU location (set the context rule):

In the Define Page Type area, select the regex context, and then add the expression (where the example displays [my regex].

Example:

Expression: [a-zA-Z0-9]{5}-[a-zA-Z0-9_]{3}._

This rule finds all pages with an SKU structure that meets the included conditions, such as this URL:
www.mysite.com/pl/pl/4270h-00x/opaska-na-wl-re

Next, set where the SKU is located on your product page. In this example, the SKU value is taken from the dataLayer[0].sku (on the page). Objects can be used in regex rules if they are available from the global window.

Cart page

The cart URL is unique. For example, this URL: www.mysite.com/pl/pl/checkout/cart/ is the only URL on the site with the string /checkout/cart. Therefore, no regex rule is needed. The page type can be detected using the URL parameter.

🚧

Notes:

  • Context rules have a 50 ms timeout, so make sure the element you are querying is available in this time frame.
  • The dynamic attribute can be a word (string, char, and so on), or it can be code that is available in the global window, for example: dataLayer[0].sku.
  • Make sure to set data parameters for product page types that require them. For example, PRODUCT or CATEGORY. If you use the Data Attribute option, you must ensure that this parameter is defined either manually or automatically within the context rule, otherwise, Page context will not be created and Dynamic Yield scripts will not work fully (say, campaigns might not be served).