Skip to content

Source: expression

Richard Cowin edited this page Mar 25, 2024 · 4 revisions

The expression source value allows for expressions to be specified in the key attribute to access data (dataLayer or other) that is bound to global window object.

Simple

It can be used for simple expressions to extract window.location.pathname

  "source": "expression",
  "key": "window.location.pathname"

Sync vs Async

If an expression based metric has an attribute value for on and the expression evaluates to a function, then that function will be treated as async and the value of the on will be used as the first argument. Promise type patterns can be used if :promise follows the async function - more on that below.

Here is an example of an async call for checking events on the document object.

  "sounce": "expression",
  "key": "document.addEventListener",
  "on": "my-event-ondocument"

Operators

Expressions can include special operators defined by :. Whenever these are used, they are specific to the type of value to the prior part of the expression. The following operators are supported:

Operator Type Description
:join array parameter is delemeter for the join
:at array parameter is index for the array
:sum array for getting the sum of numeric values embedded in the array elements. Does not take parameter
:filter array for filtering the contents of an array. the ':sum' and :join automatically filter based on presence of chained expression to right.
:is any takes a parameter to check conditionality against for use in :filter
:promise function specifies how async function is invoked.

Here is an example for pulling the event attribute from each element in the dataLayer array. It will then join the event values into a string joined by the delimiter specified (default is :join(|)). If there is an element that does not contain an event value it will not included.

  "source": "expression",
  "key": "window.dataLayer:join.event"

Here is an example for pulling the cartItem.amount from each element in the dataLayer array. It will then sum the numeric values. It will ignore any element that does not contain cartItem.amount.

  "source": "expression",
  "key": "window.dataLayer:sum.cartItem.amount"