Share a Tip

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

Mohamed_Mohamed
New Member

Hi everyone,

Problem:

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

image.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.

image.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

image.png

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

0 Karma
Get Updates on the Splunk Community!

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL

Splunk AI Assistant for SPL | Key Use Cases to Unlock the Power of SPL  The Splunk AI Assistant for SPL ...

Buttercup Games: Further Dashboarding Techniques (Part 5)

This series of blogs assumes you have already completed the Splunk Enterprise Search Tutorial as it uses the ...

Customers Increasingly Choose Splunk for Observability

For the second year in a row, Splunk was recognized as a Leader in the 2024 Gartner® Magic Quadrant™ for ...