SaaS data tracking via JS API and Web API

Track events and properties from your website

To set up triggered scenarios, you should first set up data tracking. The following data is commonly used in SaaS: sign up, subscription payment, revenue per lead, etc. This article contains some code examples to help you send required data from your website to Dashly. One more important thing is setting User ID transfer and loading information about leads who visit your website.

Read more about API possibilities in Dashly Developers.

Passing User ID to Dashly

We use User ID to combine lead’s data from different devices into one user card. As soon as a user logs in, you need to pass their User ID to us with dashly.auth() method:

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

The first argument of the method is the User ID. The second one is HMAC SHA 256 hash (The hashing text is User ID. The key is User Auth Key, which you can find in the “Settings” – “Developers” section).

Example in PHP:

$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 dashly.auth() method in Dashly Developers.

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});

The user_email variable from the code above must contain the user’s email, and the user_name variable must contain their name respectively.

Name, Email and Phone number properties

To record these properties, the code should run when you want to pass the "Name", "Email" and "Phone number" properties from your website to Dashly (on sign up, on login and when a user replies to forms on your website). Use this code to transfer only properties that 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}
]);

The user_email variable from the code above must contain the user’s email, and the user_name variable must contain their name respectively.

Recording lead events and properties 

To record other events (visit "Contacts" page, watched a video, shared a link, subscribed to the newsletter, etc.) you can use JavaScript API:

// Event tracking example with no event properties:

dashly.track('Event name');

// Event tracking example with event attributes:

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

Or Web API:

// Event tracking example with no event attributes:

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

// Event with event attributes:

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"

Dashly ID (Recorded in the dashly_uid cookie) is attached to all user cards. You can use your User ID to pass events by as well if you’ve configured User ID transfer.

JS API and Web API methods can be used to track lead properties as well.

JS API:

// 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:

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 completed" event and order-related properties

The following code should run on the order success page:

dashly.track('$order_completed', {
    '$order_amount': localStorage['lastAmount'],
    '$order_id': document.querySelector(order_id).textContent.replace(/\D/g, '')
});
dashly.identify([{ op: "delete", key: "$cart_items", value: 0 },
    { op: "delete", key: "$viewed_products", value: 0 },
    { op: "delete", key: "$cart_amount", value: 0 },
    { op: "add", key: "$revenue", value: localStorage['lastAmount'] },
    { op: "add", key: "$orders_count", value: 1 },
    { op: "update_or_create", key: "$last_payment", value: localStorage['lastAmount'] }
]);
localStorage.removeItem('lastAmount');

Import leads to Dashly

You can import your lead database to Dashly via REST API.

Example in 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, you can find it in the "Settings" – "Developers" section


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