Importing Data

  • Updated

Importing data into LianaAutomation is fairly straightforward.

Before we can import data we need to have a channel created in LianaAutomation to receive the data. Importing must be enabled for this channel.

With each import request, you can only import to one channel at a time. You can however, import multiple events for multiple persons in the same import request.

Create a channel

  • Go to LianaAutomation
  • Create a channel in Settings -> Channels
  • Give the channel a name and optionally an icon and color.
  • Give the channel a system name. This name has to be unique.
  • Mark the checkboxes for
    • “Importing enabled”
    • “Event listeners enabled”
  • Save the channel
  • Open the channel again and note the ‘Channel ID’ that is shown

Data structure

In LianaAutomation, data is represented as events that are linked to a person.

A person is identified by it’s email address, phone number, a unique key from your system or a token (cookie from a tracking script) (in this priority order). Whenever one of these matches with another person in the system, the information is updated with all the new data, leaving non specified information as is. When some information is known for separate persons inside LianaAutomation, they will be combined into 1 person containing all the history.

Events contain the actual data. A person has for example created an order, which will be an order event in LianaAutomation. This type of order we call a verb. Each event has their properties or items. These could be an id, status, date, etc. They are called items.

When data is inserted multiple times, a duplicate counter is incremented and the date of the event is updated with the last insert date. You can choose to discard creating duplicates and thus leave the date of the event to the original insert with the no_duplicates property. This also implies that no segment will be triggered when the event is a duplicate.

Here is an example of the body of an import request:

{
  "channel": "3",                       // Your channel system name or id
  "no_duplicates": false,               // When true, duplicate events are ignored
  "data": [                             // Data section (array)
    {
      "identity": {                     // Identity section
        "email": "someone@domain.com",  // Email identifier          
        "sms": "+358123456789",         // Phone number identifier
        "extra1": "abcdef",             // external identifier 1
        "extra2": "string",             // external identifier 2
        "token": "afd4345ddfe"          // cookie identifier
      },
      "events": [                       // events section (array)
        {
          "verb": "order",              // type of event
          "items": {                    // event properties
            "product": "phone",         // first property
            "status": "ordered"         // second property etc..
          }
        }
      ]
    }
  ]
}

Code samples

Using the connection sample from above you can make this request to the /v1/import endpoint. Below you can find full code samples that send some events to multiple persons.

PHP

<?php

/**
 * Account specific variables.
 *
 * These variables are given to you by or can be requested from your contact person at Liana Technologies.
 */
$user   = "USER ID";          // The user id, integer
$secret = "USER SECRET";      // An hexadecimal secret string
$url    = "REST API URL";     // The base url for your API installation
$realm  = "REST API REALM";   // The realm of your API installation, all caps alphanumeric string

/**
 * General variables
 */
$basePath    = 'rest';             // Base path of the api end points
$contentType = 'application/json'; // Content will be send as json
$method      = 'POST';             // Method is always POST

/**
 * Send a API request to LianaAutomation
 *
 * This function will add the required headers and calculates the signature for the authorization header
 *
 * @param string $path             The path of the end point
 * @param array  $data             The content body (data) of the request
 * @return mixed
 */
function send(string $path, array $data)
{
    // Import variables
    global $user, $secret, $url, $realm, $basePath, $contentType, $method;
    // Encode our body content data
    $data = json_encode($data);
    // Get the current datetime in ISO 8601
    $date = date('c');
    // md5 hash our body content
    $contentMd5 = md5($data);
    // Create our signature
    $signatureContent = implode(
        "\n",
        [
            $method,
            $contentMd5,
            $contentType,
            $date,
            $data,
            "/{$basePath}/{$path}"
        ],
    );
    $signature = hash_hmac('sha256', $signatureContent, $secret);
    // Create the authorization header value
    $auth = "{$realm} {$user}:" . $signature;

    // Create our full stream context with all required headers
    $ctx = stream_context_create([
        'http' => [
            'method' => $method,
            'header' => implode(
                "\r\n",
                [
                    "Authorization: {$auth}",
                    "Date: {$date}",
                    "Content-md5: {$contentMd5}",
                    "Content-Type: {$contentType}"
                ]
            ),
            'content' => $data
        ]
    ]);

    // Build full path, open a data stream, and decode the json response
    $fullPath = "{$url}/{$basePath}/{$path}";
    $fp = fopen($fullPath, 'rb', false, $ctx);
    $response = stream_get_contents($fp);
    $response = json_decode($response, true);
    return $response;
}

// Import Data
$response = send(
    'v1/import',
    [
        "channel" => "3",                             // The channel ID
        "no_duplicates" => false,                     // Duplicates event timestamp will be updated
        "data" => [
            [                                         // First person
                "identity" => [
                    "email" => "someone@domain.com",  // Email
                    "sms" => "+358123456789",         // Phone number
                    "extra1" => "abcdef",             // external identifier 1
                ],
                "events" => [                         // events for first person
                    [
                        "verb" => "order",            // order event
                        "items" => [
                            "product" => "phone",     // order-product: phone
                            "status" => "ordered"     // order-status: ordered
                        ]
                    ],
                    [
                        "verb" => "payment",          // payment event
                        "items" => [
                            "amount" => "599,98",     // payment-amount: 599,98
                            "currency" => "eur"       // payment-currency: eur
                        ]
                    ]
                ]
            ],
            [                                         // Second person
                "identity" => [
                    "email" => "someone2@domain.com", // Email
                    "sms" => "+358987654321",         // Phone number
                    "extra1" => "zyxwvu",             // external identifier 1
                ],
                "events" => [                         // events for first person
                    [
                        "verb" => "order",            // order event
                        "items" => [
                            "product" => "lego",      // order-product: lego
                            "status" => "delivered"   // order-status: delivered
                        ]
                    ],
                    [
                        "verb" => "message",          // message event
                        "items" => [
                            "type" => "inbound",      // message-type: inbound
                            "reason" => "compliment"  // message-reason: compliment
                        ]
                    ]
                ]
            ]
        ]
    ]
);
var_dump($response);

 

Node (Javascript)

/**
 * Dependencies
 *
 * We use the CryptoJS library for hashing and fetch for making the http call
 */
import md5 from 'crypto-js/md5.js';
import hmacSHA256 from 'crypto-js/hmac-sha256.js';
import fetch from 'node-fetch';

/**
 * Account specific variables.
 *
 * These variables are given to you by or can be requested from your contact person at Liana Technologies.
 */
var user = "USER ID";          // The user id, integer
var secret = "USER SECRET";      // An hexadecimal secret string
var url = "REST API URL";     // The base url for your API installation
var realm = "REST API REALM";   // The realm of your API installation, all caps alphanumeric string

/**
 * General variables
 */
var basePath = 'rest';             // Base path of the api end points
var contentType = 'application/json'; // Content will be send as json
var method = 'POST';             // Method is always POST

async function send(path, data) {
    // Format object to string
    data = JSON.stringify(data);
    // Get the current datetime in ISO 8601
    let date = new Date().toISOString();
    // md5 hash our body content
    let contentMd5 = md5(data).toString();
    // Create our signature
    let signatureContent = [
        method,
        contentMd5,
        contentType,
        date,
        data,
        '/' + basePath + '/' + path
    ].join('\n');
    let signature = hmacSHA256(signatureContent, secret).toString();

    // Create the authorization header value
    let auth = realm + ' ' + user + ':' + signature;

    // Build the full path and create our request with all required headers
    let fullPath = url + '/' + basePath + '/' + path;
    let response = await fetch(fullPath, {
        "method": method,
        "headers": {
            "Authorization": auth,
            "Date": date,
            "Content-md5": contentMd5,
            "Content-Type": contentType
        },
        "body": data
    });

    // Return the response
    return response.json().then(res => {
        return res;
    });
}

// Import data
send(
    'v1/pingpong',
    {
        "channel": "3",                             // The channel ID
        "no_duplicates": false,                     // Duplicates event timestamp will be updated
        "data": [
            {                                       // First person
                "identity": {
                    "email": "someone@domain.com",  // Email
                    "sms": "+358123456789",         // Phone number
                    "extra1": "abcdef",             // external identifier 1
                },
                "events": [                         // events for first person
                    {
                        "verb": "order",            // order event
                        "items": {
                            "product": "phone",     // order-product: phone
                            "status": "ordered"     // order-status: ordered
                        }
                    },
                    {
                        "verb": "payment",          // payment event
                        "items": {
                            "amount": "599,98",     // payment-amount: 599,98
                            "currency": "eur"       // payment-currency: eur
                        }
                    }
                ]
            },
            {                                       // Second person
                "identity": {
                    "email": "someone2@domain.com", // Email
                    "sms": "+358987654321",         // Phone number
                    "extra1": "zyxwvu",             // external identifier 1
                },
                "events": [                         // events for first person
                    {
                        "verb": "order",            // order event
                        "items": {
                            "product": "lego",      // order-product: lego
                            "status": "delivered"   // order-status: delivered
                        }
                    },
                    {
                        "verb": "message",          // message event
                        "items": {
                            "type": "inbound",      // message-type: inbound
                            "reason": "compliment"  // message-reason: compliment
                        }
                    }
                ]
            }
        ]
    }

).then((data) => {
    console.log(data);
});

Was this article helpful?

0 out of 0 found this helpful

Comments

0 comments

Article is closed for comments.