cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Get last day total visits using analytics API to query EUM data from Events

Mohamed.Mohamed
Creator

Hi everyone,

Problem:

Unfortunately, you cannot display data more than 10K on the dashboard using ES queries.

 

MohamedMohamed_1-1710586707223.png

So the only way to do it is from the API.
Objective:
We want automation to find the last 24h visits every day just by running a script and sending an email with the results daily using Cronjob


Solution:

I made a script to get the total number of Nusuk website visits using AppDynamics API as it’s the only way to get the number.

MohamedMohamed_2-1710587081680.png

https://docs.appdynamics.com/appd/23.x/23.12/en/extend-appdynamics/appdynamics-apis/analytics-events...

Challenges:

1- I had to find a way to automate the script to find the current date and time in UNIX format

2- Then, call the API with the right parameters to get the scrollID.

3- Lastly, call the API again to get the Visits number.


Script tasks:

  1. Get today’s and yesterday's dates in UNIX format (13 digits) -> last 3 digits should be zeros
  2. Call the first API to get the scrollid string.
  3. Recall the API with scrollid string in the API data.
  4. Grep the results.

Script:

#!/bin/bash

today=$(date +"%s.%3N" | awk '{printf "%d%03d\n", $1, substr($2,1,3)}')
yesterday=$(date -d "yesterday" +"%s.%3N" | awk '{printf "%d%03d\n", $1, substr($2,1,3)}')


scrollid=$(curl -s -X POST "<ES-IP>:9080/events/query?start=${yesterday}&end=${today}" -H"X-Events-API-Key:<your-Key>" -H"X-Events-API-AccountName:<customer-ID>" -H"Content-Type:application/vnd.appd.events+text;v=2" -H"Accept:application/vnd.appd.events+json;v=2" -d"[{ \"query\": \"SELECT * FROM web_session_records\", \"mode\": \"scroll\"}]" | grep -oP '(?<="scrollid":").*?(?=",)')

curlout=$(curl -s -i \
-H "Accept: application/vnd.appd.events+json;v=2" \
-H "Content-Type:application/vnd.appd.events+text;v=2" \
-H "X-Events-API-Key:<your-key>" \
-H "X-Events-API-AccountName:<customer-ID>" \
-d "[{ \"query\": <ES_IP>\"SELECT count(*) FROM web_session_records\", \"mode\": \"scroll\", \"scrollid\":\"$scrollid\"}]" \
-X POST "http://<ES_IP>:9080/events/query?start=${yesterday}&end=${today}")

echo "$curlout" | grep -o '"results":\[\[[0-9]*\]\]' | cut -d "[" -f3 | cut -d "]" -f1


You can get the variables from:
1- Account name

From the license page on the Contoller UI

MohamedMohamed_3-1710588281691.png

2- API key:
https://docs.appdynamics.com/appd/23.x/23.12/en/analytics/deploy-analytics-with-the-analytics-agent/... 

 


Mohamed Ashraf
Application Operations Engineer
Platform & IT Solution Ops team
Saudi Company for Visa and Travel Solutions (SVTS)

Phone: +966549574746
Email: mmahmoud.c@saudivts.com
Riyadh, Saudi Arabia



0 REPLIES 0