Pirsch Docs>Dashboard>Sessions

Sessions

22. Oct, 2021

Pirsch has the option to keep sessions alive. We do not recommend using this feature for regular websites, but it can be handy for mobile apps, single-page applications, and other types of software where you have long-running sessions. Otherwise, a session will only be kept alive if you send a page view, which is usually the desired behavior.

Extending a session prevents creating unnesseccary page views.

Extending Sessions Using the API

Sessions can be extended by sending a simple request to the API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
POST https://api.pirsch.io/api/v1/session

{
    "ip":               "123.456.789.0",
    "cf_connecting_ip": "CF-Connecting-IP header (optional)",
    "x_forwarded_for":  "X-Forwarded-For header (optional)",
    "forwarded":        "Forwarded header (optional)",
    "x_real_ip"         "X-Real-IP header (optional)",
    "dnt":              "DNT header (optional)",
    "user_agent":       "User-Agent header"
}

The data must be identical to the data used when sending a regular page view.

Extending Sessions Using Javascript

It’s possible to extend a session from the browser, without adding anything. Here is a snippet keeping the session alive. It calls the session endpoint once a minute to make sure it’s kept alive.

1
2
3
4
5
6
7
const identificationCode = "website_identification_code";

setInterval(() => {
    const req = new XMLHttpRequest();
    req.open("POST", `https://api.pirsch.io/session?nc=${new Date().getTime()}&code=${identificationCode}&url=${encodeURIComponent(location.href.substr(0, 1800))}`);
    req.send();
}, 60_000);