This step is only required if you use a client ID + secret. A single access key allows you to skip the step, but it cannot be used to read statistics as with the other method. For write-only clients, we recommend using an access key.
Replace the CLIENT_ID
and CLIENT_SECRET
in the body. The access token in the response will be valid for 15 minutes.
curl --location --request POST 'https://api.pirsch.io/api/v1/token' \
--header 'Content-Type: application/json' \
--data-raw '{
"client_id": "CLIENT_ID",
"client_secret": "CLIENT_SECRET"
}
'
Replace the ACCESS_TOKEN
with the actual access token from the previous request or use the access key created on the settings page. You also need to adjust the domain example.com
in the request.
curl --location --request POST 'https://api.pirsch.io/api/v1/hit' \
--header 'Authorization: Bearer ACCESS_TOKEN' \
--header 'Content-Type: application/json' \
--data-raw '{
"url": "https://example.com/pirsch/page-view",
"ip": "91.36.189.125",
"accept_language": "en,en-US",
"user_agent": "Mozilla/5.0 (X11; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0",
"referrer": "https://google.com",
"screen_width": 1920,
"screen_height": 1080
}
'
The same request as above, but PHP with curl.
<h1>Pirsch curl Demo</h1>
<p>This page sends a page view using the Pirsch API and curl.</p>
<?php
// Configure the Pirsch API access key and get the current URL including the hostname.
$token = 'pa_6ldFoOhs7VgBFMN1vn7wIdXpFclvrjF8T8MApCa088xnl';
$url = getRequestURL();
// Send the page view request.
pirschPageView($url, $token);
function pirschPageView($url, $token) {
$ip = getHeader('REMOTE_ADDR');
$lang = getHeader('HTTP_ACCEPT_LANGUAGE');
$userAgent = getHeader('HTTP_USER_AGENT');
$referrer = getReferrer();
$secChUa = getHeader('HTTP_SEC_CH_UA');
$secChUaMobile = getHeader('HTTP_SEC_CH_UA_MOBILE');
$secChUaPlatform = getHeader('HTTP_SEC_CH_UA_PLATFORM');
$secChUaPlatformVersion = getHeader('HTTP_SEC_CH_UA_PLATFORM_VERSION');
$secChWidth = getHeader('HTTP_SEC_CH_WIDTH');
$secChViewportWidth = getHeader('HTTP_SEC_CH_VIEWPORT_WIDTH');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.pirsch.io/api/v1/hit');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Authorization: Bearer $token",
'Content-Type: application/json',
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"url\":\"$url\",\"ip\":\"$ip\",\"accept_language\":\"$lang\",\"user_agent\":\"$userAgent\",\"referrer\":\"$referrer\",\"sec_ch_ua\":\"$secChUa\",\"sec_ch_ua_mobile\":\"$secChUaMobile\",\"sec_ch_ua_platform\":\"$secChUaPlatform\",\"sec_ch_ua_platform_version\":\"$secChUaPlatformVersion\",\"sec_ch_width\":\"$secChWidth\",\"sec_ch_viewport_width\":\"$secChViewportWidth\"}");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$response = curl_exec($ch);
curl_close($ch);
}
function getRequestURL() {
return 'http'.(getHeader('HTTPS') != '' ? 's' : '').'://'.getHeader('HTTP_HOST').getHeader('REQUEST_URI');
}
function getHeader($name) {
if (isset($_SERVER[$name])) {
return $_SERVER[$name];
}
return '';
}
function getReferrer() {
$referrer = getHeader('HTTP_REFERER');
if (empty($referrer)) {
$params = array(
'ref',
'referer',
'referrer',
'source',
'utm_source'
);
foreach($params as $key) {
if (isset($_GET[$key])) {
$referrer = $_GET[$key];
if (!empty($referrer)) {
return $referrer;
}
}
}
}
return $referrer;
}