Skip to content

Source: on‐async

Richard Cowin edited this page Dec 5, 2023 · 2 revisions

The on-async source is used for custom expressions that are async in nature. This will allow async expressions with the following format

  <key>.on(<on>, (...params)=> process(params))

As an example, direct coded Segment integration would look like.

  window.analytics.on('track', (...params)=> process(params[0], params[1]))

This would be represented in the metric as:

{
  "source": "on-async",
  "key": "window.analytics",
  "on": "track",
  "extract": {
    "expression": "params:at(0)"
  }
}

The params:at(0) in the extract clause would provide the event id from segment. This could be used to bind to an audience attribute segment.event (or other) as in:

{
  "source": "on-async",
  "key": "window.analytics",
  "on": "track",
  "extract": {
    "expression": "params:at(0)"
  },
  "action": "bind",
  "tag": "segment.event"
}

Or it could be conditionally used to drive an event segment.event.addToCart (or other) as in:

{
  "source": "on-async",
  "key": "window.analytics",
  "on": "track",
  "extract": {
    "expression": "params:at(0)"
  },
  "action": "event",
  "apply": [
    {"when": "addToCart", "tag": "segment.event.addToCart"}
  ]
}