For WordPress, setting up server analytics is the same process as setting up client analytics. If you already did that, you can move to the next step.
Install the Package
Download the package from NPM using the command line.
npm install @darkvisitors/sdk
Copy Your Access Token
- Navigate to the Projects page
- Select your project
- Click Settings
- Copy your access token
Initialize the Client
Create a new instance of DarkVisitors
with your access token.
import { DarkVisitors } from "@darkvisitors/sdk"
const darkVisitors = new DarkVisitors(YOUR_ACCESS_TOKEN)
Start Tracking Visits
In the endpoints where you serve your pages, call trackVisit
for each incoming pageview request. If you can, add this in middleware to track incoming requests to all pages from a single place. See an example on NPM.
darkVisitors.trackVisit(request)
Use the REST API to track visits from any codebase or programming language.
Copy Your Access Token
- Navigate to the Projects page
- Select your project
- Click Settings
- Copy your access token
Start Tracking Visits
In the endpoints where you serve your pages, make an HTTP request to the REST API for each incoming pageview request. If you can, add this in middleware to track incoming requests to all pages from a single place.
URL |
URL |
https://api.darkvisitors.com/visits |
HTTP Method |
POST |
Headers |
Authorization |
A bearer token with your project's access token (e.g. Bearer 48d7-fc44-4b30-916b-2a59 ). |
Content-Type |
This needs to be set to application/json |
Body |
request_path |
The URL path of the incoming pageview request |
request_method |
The HTTP method of the incoming pageview request (e.g. GET , POST , etc.) |
request_headers |
The HTTP headers of the incoming pageview request, as a key-value object. |
Example
curl -X POST https://api.darkvisitors.com/visits \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{
"request_path": "'"${request.path}"'",
"request_method": "'"${request.method}"'",
"request_headers": "'"${request.headers}"'"
}'
Tips
- Make this call non-blocking to avoid adding latency to your page response.
- Handle errors in a way that doesn't make your page response fail too.
- Strip out any sensitive HTTP headers you don't want to send.
The Shopify integration is in the works. If you want early access, please contact us.
Copy Your Access Token
- Navigate to the Projects page
- Select your project
- Click Settings
- Copy your access token
Start Tracking Visits
Define a function that makes an HTTP request to the REST API with detail about an incoming pageview request.
async def track_visit_in_dark_visitors(request):
try:
async with aiohttp.ClientSession() as session:
await session.post(
"https://api.darkvisitors.com/visits",
headers={
"Authorization": f"Bearer {YOUR_ACCESS_TOKEN}",
"Content-Type": "application/json",
},
json={
"request_path": request.url.path,
"request_method": request.method,
"request_headers": dict(request.headers),
}
)
except Exception:
pass
In the endpoints where you serve your pages, call the function for each incoming pageview request. If you can, add this in middleware to track incoming requests to all pages from a single place.
asyncio.create_task(track_visit_in_dark_visitors(request))
Tips
- If you change this code, keep it non-blocking to avoid adding latency to your page response.
- If you change this code, keep handling errors in a way that doesn't make your page response fail too.
- Strip out any sensitive HTTP headers you don't want to send.
Copy Your Access Token
- Navigate to the Projects page
- Select your project
- Click Settings
- Copy your access token
Start Tracking Visits
Define a function that makes an HTTP request to the REST API with detail about an incoming pageview request.
function track_visit_in_dark_visitors() {
$curl = curl_init('https://api.darkvisitors.com/visits');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $YOUR_ACCESS_TOKEN,
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode([
'request_path' => $_SERVER['REQUEST_URI'],
'request_method' => $_SERVER['REQUEST_METHOD'],
'request_headers' => getallheaders(),
], JSON_UNESCAPED_SLASHES),
CURLOPT_NOSIGNAL => true,
CURLOPT_TIMEOUT_MS => 50,
]);
@curl_exec($curl);
curl_close($curl);
}
In the endpoints where you serve your pages, call the function for each incoming pageview request. If you can, add this in middleware to track incoming requests to all pages from a single place.
track_visit_in_dark_visitors();
Tips
- If you change this code, keep it non-blocking to avoid adding latency to your page response.
- If you change this code, keep handling errors in a way that doesn't make your page response fail too.
- Strip out any sensitive HTTP headers you don't want to send.