How can I retrieve event data on a daily basis?

Hello,
Page view values can be obtained on a daily basis.
like this.
https://simpleanalytics.com/hostname.json?version=5&fields=histogram&start=2024-03-25&end=2024-04-25&pages=pageurl

How can I retrieve event data on a daily basis as well?

It does not have to be in json format.

I tried to get the day-by-day values in a javaScript loop with this url,

https://simpleanalytics.com/hostname.json?version=5&start=2024-04-26&end=2024-04-26&events=eventname

but that resulted in an error, so a JavaScript sample code would be fine.

Thank you.

This should work: Stats API | Simple Analytics Docs.

Get event counts by adding the events=...-param to your request. To get the counts of the events (visit_homepage, popup_replace_show, popup_replace_close), you can run this request:

https://simpleanalytics.com/simpleanalytics.com.json?version={{ site.api_version }}&start=yesterday&end=today&timezone=Europe/Amsterdam&events=visit_homepage,popup_replace_show,popup_replace_close
{
  "events": [
    {
      "name": "visit_homepage",
      "total": 233
    },
    {
      "name": "popup_replace_show",
      "total": 117
    },
    {
      "name": "popup_replace_close",
      "total": 61
    }
  ]
}

If you use events=*, all events are returned (limited to 1000 events).

What is the error you get?

Thank you for your reply.
That URL would return a total within a term, wouldn’t that? ,
but I would like to get the value on a daily basis.
like this…

  {
        "date": "2024-03-25",
        "pageviews": 710,
        "visitors": 240
    },
    {
        "date": "2024-03-26",
        "pageviews": 186,
        "visitors": 81
    },
    {
        "date": "2024-03-27",
        "pageviews": 63,
        "visitors": 27
    },

So, I tried day to day loop with JavaScript.

Regarding error, I tried this JavaScript Code, but this code returns error ,
due to (probably) simple analytics api/url returns error that too may calls in a short period time?

// URL
const eventName = “Ad1”;
const startDate = new Date(“2024-04-26”);
const endDate = new Date(“2024-04-29”);

// 開始日から終了日までの各日の結果を取得して出力
for (let currentDate = new Date(startDate); currentDate <= endDate; currentDate.setDate(currentDate.getDate() + 1)) {
// currentDateの値を固定するために新しい変数にコピー
const requestDate = new Date(currentDate);
const url = https://simpleanalytics.com/publickey1.jp.json?version=5&start=${requestDate.toISOString().split("T")[0]}&end=${requestDate.toISOString().split("T")[0]}&events=${eventName};

getEventData(url)
.then(total => {
// 非同期関数の中で使用する日付はrequestDateを使用
console.log(${requestDate.toISOString().split("T")[0]}: Total:, total);
})
.catch(error => console.error(${requestDate.toISOString().split("T")[0]}: Error fetching data:, error));
}

// イベントデータを取得する関数
function getEventData(url) {
return fetch(url)
.then(response => response.json())
.then(data => {
const events = data.events;
const { total } = events[0];
return total;
});
}

I tried again, and above code will work.
So, my question resolved myself, probably.
Thank you anyway. I will close this.

1 Like