Back

How to configure an advanced Webhook - Send Custom Request action using Template Variables for Keen.io API

PRE-REQUISITES

Overview

Submit an http request to Keen.io to record multiple purchase events for a single transaction. Whole request body is a JSON message.

Vendor Requirements

Documentation: Keen IO Data Collection API

 

Request URL

https://api.keen.io/API_VERSION/projects/PROJECT_ID/events

API Request Body Example:

{
    "purchases": [
        {
            "item": "Golden gadget",
            "price": 25.50,
            "transaction_id": "f029342"
        },
        {
            "item": "Different gadget",
            "price": 17.75,
            "transaction_id": "f029342"
        }
    ],
    "transactions": [
        {
            "id": "f029342",
            "items": 2,
            "total": 43.25
        }
    ]
}

 

Action Implementation

Method Field

Set to “POST”.

URL Field

Set to “{{url_template}}”.

 

URL can either be hard coded (with embedded api version and project ID) or template based. The latter approach is taken to allow greater flexibility with data layer attributes. See templates section below.

Body Content Type

Select option “application/json”.

Body Data

Select “Body” option and provide a template reference.

Name Value
Body {{json_template}}

Template Variables

Set name and value pairs to be referenced and replaced in templates. Variable values are custom values in this example, but can easily be provided as dynamic data layer attributes.

 

Name Value Note
transactionId f029342  
transactionItemCount 2  
transactionTotalPrice 43.25  
purchases.item Keen.io Purchase Items Set of Strings attribute
purchases.price Keen.io Purchase Prices Set of Strings attribute
apiVersion 3.0  
projectId 98765  

Variables are internally translated to JSON and made available to all templates.

Resulting JSON structure:

{
  "purchases": [
    {
      "price": "17.75",
      "item": "Different gadget"
    },
    {
      "price": "25.50",
      "item": "Golden gadget"
    }
  ],
  "apiVersion": "3.0",
  "transactionId": "f029342",
  "transactionItemCount": "2",
  "transactionTotalPrice": "43.25",
  "projectId": "98765"
}

 

Templates

Name Value
url_template https://api.keen.io/{{apiVersion}}/projects/{{projectId}}/events
json_template
{
    "purchases": [
        {{#purchases}}
        {
            "item": "{{item}}",
            "price": {{price}},
            "transaction_id": "{{transactionId}}"
        }{{#iter.hasNext}}, {{/iter.hasNext}}
        {{/purchases}}
    ],
    "transactions": [
        {
            "id": "{{transactionId}}",
            "items": {{transactionItemCount}},
            "total": {{transactionTotalPrice}}
        }
    ]
}

Templates Rendered

Internally templates get rendered (see below) and their content injected where referenced (i.e.: URL field and Body).

 

Name Value
url_template https://api.keen.io/3.0/projects/98765/events
json_template
{
    "purchases": [
        {
            "item": "Different gadget",
            "price": 17.75,
            "transaction_id": "f029342"
        },
        {
            "item": "Golden gadget",
            "price": 25.50,
            "transaction_id": "f029342"
        }
    ],
    "transactions": [
        {
            "id": "f029342",
            "items": 2,
            "total": 43.25
        }
    ]
}

Action Configuration Screenshot

 example