SaaS data tracking via Web API

Track user events and properties from the backend of your website

To configure auto messages, you should track user data. The following events and properties will be interesting for SaaS: sign up, subscription payment, revenue per lead, etc. Here are examples of code snippets which will help to send required data from selected pages to Dashly. One more important thing is setting User ID transfer and loading information about leads who visit your website before you the service installation.

Read more about all possibilities of API using in the developers settings.

Setting User ID transfer

We use User ID to combine leads who enter your website through different devices. As soon as the lead is logged in your system, you should send us his ID by using this command (example):

dashly.auth('25', 'hmac-sha256-hash');

The first argument is the User ID. The second is HMAC SHA 256 hash (the hashing text is User ID, the key is User Auth Key, you can find it in the admin panel).

PHP example:

$userId = '…';
$hash = hash_hmac('sha256', $userId, 'User-Auth-Key' );
echo "dashly.auth('".$userId."', '".$hash."')"

Read more about User ID transfer in this article and find out about method in the developers section.

Sign up and Log in events

The following code should run when one logs in or signs up:

dashly.track("$registered", {"$email": user_email, "$name": user_name});
dashly.track("$authorized", {"$email": user_email, "$name": user_name});

use lead's email instead of user_email in both cases and lead's name instead of user_name.

Name, Email and Phone number properties

To record these properties the code should run each time when one fills in "Name", "Email" and "Phone number" fields on your site (sign up, log in, subscribe and other feedback forms). Use this code to transfer only those properties which have input fields in the form. Example: if you send a form which contains "Name" and "Email" only, then remove $phone property from a code:

dashly.identify([
    {op: "update_or_create", key: "$email", value: user_email},
    {op: "update_or_create", key: "$phone", value: user_phone},
    {op: "update_or_create", key: "$name", value: user_name}
]);

Copy code

 

Enter lead's email instead of user_email, lead's phone number instead of user_phone and lead's name instead of user_name.

Lead events and attributes transfer

For transferring any other events (visit "Contacts" page, watched a video, shared a link with friends, subscribed to newsletter, etc.) you can use JS API methods.

// No parameters

dashly.track('Event name');

// With parameters

dashly.track('Event name', {
  'param1': 'value1',
  'param2': 340,
  'param3': '2014-05-23T05:12:45',
  'param4': false,
  'param5': ['key1', 'key2', 'key3']
});

or via Web API methods:

// No parameters

curl -X POST \
  --data-urlencode "event=Order Created" \
  --data-urlencode "auth_token=XXX" \
  "https://api.dashly.io/v1/users/{id}/events"

// With parameters

curl -X POST \
  --data-urlencode "event=Order Created" \
  --data-urlencode 'params={"item": "chicken"}' \
  --data-urlencode "auth_token=XXX" \
  "https://api.dashly.io/v1/users/{id}/events"

where ID is User ID from our system (user ID is placed in dashly_uid cookie). You can also use User ID if you configured leads bonding. More about track and events methods.

In order to set lead properties (identification) you can also use JS API and Web API methods. JS API example:

// Simplified format:

dashly.identify({'$name': 'Maks', 'myProp': 'myValue'});

// Complex format:

dashly.identify([
  {op: 'add', key: 'segment1', value: 1},
  {op: 'update_or_create', key: 'segment2', value: 1},
]);

Web API example:

curl -X POST \
  --data-urlencode "operations=[{op: "update_or_create","key: "$name", value: "Maks"}]" \
  --data-urlencode "auth_token=XXX" \
  "https://api.dashly.io/v1/users/{id}/props"

Order paid event

The following code should run on a payment confirmation page:

dashly.track("$order_paid", {"$order_id": order_id});

replace order_id with order ID.

Last order amount property

The following code should run on a payment confirmation page:

dashly.identify([
    {op: "update_or_create", key: "$last_payment", value: order_amount},
]);   

replace order_amount with order amount.

Revenue from a lead property

The following code should run on a payment confirmation page:

dashly.identify([
    {op: "add", key: "$profit", value: order_amount}
]);

replace order_amount with order amount.

Orders total number property

The following code should run on a payment confirmation page:

dashly.identify([
    {op: "add", key: "$orders_count", value: 1}
]);

replace order_amount with order amount.

Import leads to Dashly

In order to migrate leads from some other platform to Dashly you can use two ways: import via interface and import via Web API. Read about import via interface here.

Code snippet for importing leads via PHP:

$users = array(
	array('id' => 123, 
		  'email' => 'mail1@mail.ru', 
		  'phone' => '891xxxxxx',
		  'name' => 'Test'
	),
	array('id' => 456,
		  'email' => 'mail2@mail.ru',
		  'phone' => '891xxxxxx',
		  'name' => 'Test2'
	)
);
$auth_token = 'xxx'  # TODO: ADD YOUR AUTH_TOKEN HERE, его можно найти в разделе Настройки > API КЛЮЧИ


for ($i =0; $i < count($users); ++$i ) {
	$url = 'http://api.dashly.io/v1/users/'.$users[$i]['id'].'/props?auth_token='.$auth_token;
	$operations = json_encode(array(
			array('op' => 'update_or_create',
				  'key' => '$email',
				  'value' => $users[$i]['email']
			),
			array('op' => 'update_or_create',
				  'key' => '$phone',
				  'value' => $users[$i]['phone']
			),
			array('op' => 'update_or_create',
				  'key' => '$name',
				  'value' => $users[$i]['name']
			),
		)
	);
	
	$result = file_get_contents($url, false, stream_context_create(array(
	  'http' => array(
		'method'  => 'POST',
		'header'  => 'Content-type: application/x-www-form-urlencoded',
		'content' => http_build_query(array('operations' => $operations,'by_user_id'  => 'true')),
	  )
	)));
	
	
	print $result;
Powered by Dashly