A Great Tool for Website Operators — Use API to subscribe Monitor Key Web Traffic Metrics via Email

Cuoji De
Kyligence
Published in
4 min readNov 21, 2022

Kevin, a website operations engineer, is responsible for managing the user operations of the company’s website. Active Users, User Views, and other key metrics are critical to website operation and management. By browsing the metrics catalog on Kyligence Zen, Kevin can view the metric data he follows at any time. But he wants to regularly receive daily changes in the metrics.

In this article, let’s take a look at how Kevin subscribes to and alerts on the key metrics related to user activation using the API provided by Kyligence Zen, a one-stop-shop metrics platform, to keep an eye on the metric data.

Metrics Subscription

Kyligence Zen provides flexible APIs, enabling users to subscribe key metrics via various tools, such as email. The diagram below shows the main process of how users call the Kyligence Zen API to query metrics. Let’s follow Kevin, to see how he subscribes his metrics via API!

Kyligence Zen API invoking process

Identify key metrics

Firstly, Kevin browses the metrics catalog to identify a few key metrics to follow.

Active Users / User Views (UV) / Page Views (PV) and DOD

Next, he selects the metrics on the Kyligence Zen interface, clicks > Copy cURL code on the top right corner of the metrics card and gets the API key.

Select the metrics to copy the cURL code
Create the Key needed to invoke the API

Then he pastes the code he just obtained and replaces the information after Apikey with his API Key.

curl -X POST https://zen-cn.kyligence.io/insight/api/v1/metrics/query \
-H 'Content-Type: application/json;charset=utf-8' \
-H 'Authorization: Apikey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' \
-d '{
"metrics": [
"PV"
],
"dimensions": [
"event_date",
"event_type"
],
"filters": [],
"orders": []
}'

Add subscription email

Then, Kevin adds his email address to the mailing list. By running the Python code below to call the API, he can receive emails with the data of subscribed metrics.

The sample code used to get the results of the metrics by invoking the API and sending them by email is as follows, where we can see “Active Users — E-Commerce”, which is the key metric that Keven follows.

#! /usr/bin/python3
# -*- coding: UTF-8 -*-
...

class TestMail(object):

def __init__(self):
...
self.headers = {
'Content-Type': 'application/json;charset=utf-8',
'Authorization': 'Apikey xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx',
}

def send_mail(self, mail_content):
...

def callZenAPI(self, payload):

json_data = json.dumps(payload)
response = requests.post('https://zen.kyligence.io/insight/api/v1/metrics/query', headers=self.headers,
data=json_data)

api_data = json.loads(response.content)
return api_data

def convertToHtml(self,ks, vs):
...

def getAPIResult(self,mname,api_data):
...

if __name__ == "__main__":
send = TestMail()

#Metrics List
metricsArray = [
{
"metrics": [
"Active Users"
],
"dimensions": [
"event_date"
],
"filters": [{
"operator": "IN",
"member": "event_date",
"values": ["2020-10-27"]
}],
"orders": []
}
#...
]
...

for m in metricsArray:
mname = m['metrics'][0]
title.append(mname)
api_data = send.callZenAPI(m)
mresult = send.getAPIResult(mname, api_data)
result.append(mresult)

mail_content = "<p><h1>2020-12-27 Website Operation Metrics: </h></p>" + send.convertToHtml(title, result)
send.send_mail(mail_content)

As for subscribing to the key metrics of website operations via email, please contact us to get the source code.

Receive daily email alerts

In this way, Operations Engineer Kevin will regularly receive daily emails containing data on the key operating metrics of the website.

Diagram of an example of metrics subscription email

Metrics Alert

Active Users, UV, and PV are the core metrics that operations staff pay close attention to. But it is not enough to receive regular reports of metric values. By setting a threshold value, alarm notifications can be triggered if the data fluctuation reached a certain degree. Once Kevin receives the email notification, he can start monitoring the active data in real-time. His working efficiency can be improved.

Next, Kevin can create scheduled tasks to call APIs every other hour. An alert email will be sent to Kevin if the predefined threshold value is reached so he can adjust the operations strategy accordingly.

Experience Kyligence Zen Now

The E-Commerce PV and UV Day-to-Day Analysis Metrics have been published in the Kyligence Zen Metrics Template market. You may click the link and reproduce the use case in your own Kyligence Zen account.

--

--