This page revovles around how to use HTTP REST API requests in order to track page views and events. You might wanna check first if we have a library for the backend language that you are using like C# or Node.js. You can look at the following link to find out which languages we currently are offering libraries for.
Regardless of which endpoint you are trying to use in the REST API, you will always have to include the API Key in the Authorization
Header. Here is an example how this could look like:
To find the API Key for your website, you have to navigate Websites
> Edit website
> Left side
. On right side of the API Key, you can click to copy the API Key, including the header name. There is also the functionality to regenerate your API key.
The most common approach for using the Backend API is with the following flow:
This js object shows you what information you need from your frontend before sending it to your backend endpoint:
const getPayload = () => {
return {
"ScreenWidth": window.screen.width,
"ScreenHeight": window.screen.height,
"Language": window.navigator.language,
"Referrer": document.referrer,
"Absolute": document.location.href,
};
}
Depending on whether you just want to send a page view
or event
you also have to include the event name and parameters in the above data.
After you received the data from the frontend you have to fill in the rest of needed data on your backend, look up the corresponding endpoint to see more details:
Here is an example of how to call our API from your Backend after you received the payload from your Frontend as seen above.
Headers:
Authorization: Sqjr2..(Your API Key as seen above)
Body (JSON string):
{
"Metrics": {
"Browser": {
"IP": "91.48.119.123 (From your server framework)",
"UserAgent": "Mozilla/5.0... (From your server framework / headers)"
},
"Identify": {
"ClientId": null
},
"Language": {
"Code": "en (From your frontend window.navigator.language)"
},
"Location": {
"CountryCode": "AF (This entire section requires you to have a geolocation service to fetch for the IP, or if you are using a reverse proxy service like Cloudflare, you can enable special headers providing you with these information on the fly.",
"ContinentCode": "AF",
"City": "test",
"Region": null,
"RegionCode": null,
"PostalCode": "33100",
"Timezone": null
},
"Page": {
"Absolute": "https://app.sitesights.io/analytics (From your frontend document.location.href)"
},
"Referrer": {
"Absolute": "(From your frontend document.referrer)"
},
"Screen (From your frontend window.screen.width & window.screen.height)": {
"Width": 200,
"Height": 200
}
}
}
Currently there are these features to the Serverside API:
You can see details about all REST API Endpoints here: