Source: expression - evolv-ai/metrics GitHub Wiki
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"
Macro 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. |
:keys | object | returns an array of all keys of the object |
:values | object | returns an array of all values (equivalent to object[key]) of the object |
:promise | function | specifies how async function is invoked. note: only used for async functions and must be accompanied with an on binding. |
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"
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"
Infix Expression Operators
Arithmetic operations are also available to apply to numeric values within a common object.
The syntax to specify an infix operation is to use ()
after a .
. This indicates that the root attribute of each operand is based on the object value preceding the .
.
The following operators are supported:
Operator | Type | Description |
---|---|---|
+ | number | Addition |
- | number | Subtraction |
* | number | Multiplication |
/ | number | Division |
Here is an example for of adding the window.cart.subtotal
to the window.cart.taxes
.
"source": "expression",
"key": "window.cart.(subtotoal+taxes)"
Here is an example for of multiplying the window.cart.count
to the window.cart.price
. This is a rather simplified usage - see next section for something more realistic.
"source": "expression",
"key": "window.cart.(count*price)"
Complex Examples
If we were to combine the array operators and infix mechanism, we can do complex operations.
Summing cost of all phones in cart
"source": "expression",
"key": "window.cart.products:filter(type,"phone"):sum.(count*price)"