Lookups

The LOOKUP tells the system what value to return.

{
  "type": LOOKUP_TYPE,
  "value": LOOKUP_VALUE,
  "filters": LOOKUP_FILTERS,   // optional
  "override": false            // optional: defaults to false
}

LOOKUP_TYPE

The LOOKUP_TYPE is one of:

  • const
  • var
  • selector

const

The string value set in LOOKUP_VALUE is returned.

{
  "type": "const",
  "value": "USD"
}

Shorthand. Instead of providing an object simply set a string (or number):

"USD"

Note that you cannot supply string values that include a period as these will be treated as var in the shorthand.

var

The value of the Javascript variable in TO_VALUE is returned. By default the "window" object is used as the base, but if the TO_VALUE starts with "state." the internal Fanplayr state object is used.

The following would return the value of "window.location.pathname".

{
  "type": "var",
  "value": "location.pathname"
}

You could also use a shorthand. The fact there is a period in the string tells the lookup that we are looking for a variable.

"location.pathname"

From the currrent state

You can also get a variable from the current state. It is worth knowing the order of scraping here. The following example could be used to set the product SKU to the already scraped product ID.

Note that starting the value with "state." looks at our internal state, not a variable on the window object.

{
  "type": "var",
  "value": "state.page.product.id"
}

Again a shorthand is available. If it starts with "state" it will look in this variable.

"state.page.product.id"

In loops

When in a loop-item, for example scraping the cart, you may want to refer to variables set on the current object you are defining. For example, if the page has line item prices, rather than single per quantity prices, we may need to divide this price by the current quantity (see filter "op"). To get the current line item quantity:

{
  "type": "var",
  "value": "$self.quantity"
}

Again, knowing the order of scraping is useful here.

Arrays (and objects)

It is possible to get a value from an array. For example, if we had the following on the window:

dataLayer = [
  { pageType: "category" }
]

We could get the value we want using:

{
  "type": "var",
  "value": "dataLayer[0].pageType"
}

If we didn't know where in an array there may be a value:

dataLayer = [
  { gtm: 10 },
  { pageType: "category" }
]

We could get the value we want using (note the "[n]"):

{
  "type": "var",
  "value": "dataLayer[n].pageType"
}

We can also use this with an object (using the same lookup) such as:

dataLayer = {
  firstData: {
    gtm: 10
  },
  secondData: {
   pageType: "category"
  }
}

selector

By using selector we are calling jQuery.

{
  "type": "selector",
  "value": "span[itemprop=price] .sp_amt"
}

Is as if we are getting the value returned from:

jQuery('span[itemprop=price] .sp_amt');

To use these values we generally need to extract information using filters.

You can also use a shorthand here. The "$" at the beginning let's the parser know it's meant to be a selector.

"$('span[itemprop=price] .sp_amt')"

In loops

Note that there is a magic LOOKUP_VALUE "$self", which refers to the current jQuery element. This is only used with looping contexts such as cart.data.

{
  "type": "selector",
  "selector": "$self"
}

LOOKUP_FILTERS

See Filters section.

Override

By default, if a value has already been set, any other setting of this value is ignored. You can set override to true in any "to" statement to force setting of a new value.

results matching ""

    No results matching ""