Skip to content

Commit

Permalink
Added funnel endpoints, added options to extending sessions, updated …
Browse files Browse the repository at this point in the history
…dependencies.
  • Loading branch information
Kugelschieber committed Aug 23, 2024
1 parent 8a1542c commit 2e3ff4b
Show file tree
Hide file tree
Showing 56 changed files with 505 additions and 240 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## 2.0.0

* added funnel endpoints
* added options to extending sessions
* updated dependencies

## 1.12.0

* added regional statistics
Expand Down
50 changes: 25 additions & 25 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 33 additions & 16 deletions demo.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,28 @@
require_once 'src/Client.php';
require_once 'src/Filter.php';

$clientID = '';
$clientSecret = '';
$client = new Pirsch\Client($clientID, $clientSecret, 5.0, 'https://localhost.com:9999');

try {
$client->hit();
print '<p>Hit sent!</p>';
} catch(Exception $e) {
print '<p>An error occurred while sending the hit: </p>'.$e->getMessage();
}

try {
$client->event('PHP', 42, ['hello' => 'world']);
print '<p>Event sent!</p>';
} catch(Exception $e) {
print '<p>An error occurred while sending the event: </p>'.$e->getMessage();
// Configuration
$clientID = '9EisID5H4jTpSwrxHiTrHEstmn2isNH0';
$clientSecret = 'Vw2guiFFahW7KemRCFIYxkJF9mzULgx8Q6HMQrEDhRclQstphklHPgBfo4BL0lix';
$baseURL = Pirsch\Client::DEFAULT_BASE_URL; // https://localhost.com:9999
$sendData = false;

$client = new Pirsch\Client($clientID, $clientSecret, Pirsch\Client::DEFAULT_TIMEOUT, $baseURL);

if ($sendData) {
try {
$client->hit();
print '<p>Hit sent!</p>';
} catch(Exception $e) {
print '<p>An error occurred while sending the hit: </p>'.$e->getMessage();
}

try {
$client->event('PHP', 42, ['hello' => 'world']);
print '<p>Event sent!</p>';
} catch(Exception $e) {
print '<p>An error occurred while sending the event: </p>'.$e->getMessage();
}
}

try {
Expand Down Expand Up @@ -148,6 +154,17 @@
$keywords = $client->keywords($filter);
var_dump($keywords);
echo '<br /><br />';

$funnel = $client->listFunnel($filter);
var_dump($funnel);
echo '<br /><br />';

foreach ($funnel as $f) {
$filter->funnel_id = $f->id;
$data = $client->funnel($filter);
var_dump($data);
echo '<br /><br />';
}
} catch(Exception $e) {
print '<p>An error occurred while reading the statistics: </p>'.$e->getMessage();
}
69 changes: 46 additions & 23 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Client {
const DEFAULT_BASE_URL = 'https://api.pirsch.io';
const DEFAULT_TIMEOUT = 5.0;

const AUTHENTICATION_ENDPOINT = '/api/v1/token';
const HIT_ENDPOINT = '/api/v1/hit';
Expand Down Expand Up @@ -44,6 +45,8 @@ class Client {
const TAG_KEYS_ENDPOINT = "/api/v1/statistics/tags";
const TAG_DETAILS_ENDPOINT = "/api/v1/statistics/tag/details";
const KEYWORDS_ENDPOINT = '/api/v1/statistics/keywords';
const LIST_FUNNEL_ENDPOINT = "/api/v1/funnel";
const FUNNEL_ENDPOINT = "/api/v1/statistics/funnel";

const REFERRER_QUERY_PARAMS = array(
'ref',
Expand All @@ -57,7 +60,7 @@ class Client {
private $clientSecret;
private $client;

function __construct($clientID, $clientSecret, $timeout = 5.0, $baseURL = self::DEFAULT_BASE_URL) {
function __construct($clientID, $clientSecret, $timeout = self::DEFAULT_TIMEOUT, $baseURL = self::DEFAULT_BASE_URL) {
$this->clientID = $clientID;
$this->clientSecret = $clientSecret;
$this->client = new \GuzzleHttp\Client([
Expand Down Expand Up @@ -86,7 +89,7 @@ function hit($retry = true) {
]);
return json_decode($response->getBody());
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->hit(false);
} else {
Expand Down Expand Up @@ -139,10 +142,10 @@ function pageview(HitOptions $data, $retry = true) {
]);
return json_decode($response->getBody());
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->pageview($data, false);
} else if ($e->getResponse()->getStatusCode() != 200) {
} else if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error sending page view: '.$e->getResponse()->getBody());
}
}
Expand Down Expand Up @@ -195,38 +198,50 @@ function event($name, $duration = 0, $meta = NULL, HitOptions $data = NULL, $ret
]);
return json_decode($response->getBody());
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->event($name, $duration, $meta, $data, false);
} else if ($e->getResponse()->getStatusCode() != 200) {
} else if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error sending event: '.$e->getResponse()->getBody());
}
}

return null;
}

function session($retry = true) {
function session(HitOptions $data, $retry = true) {
try {
if (is_null($data)) {
$data = new HitOptions;
}

$data->ip = $this->isEmpty($data->ip) ? $this->getHeader('REMOTE_ADDR') : $data->ip;
$data->user_agent = $this->isEmpty($data->user_agent) ? $this->getHeader('HTTP_USER_AGENT') : $data->user_agent;
$data->sec_ch_ua = $this->isEmpty($data->sec_ch_ua) ? $this->getHeader('HTTP_SEC_CH_UA') : $data->sec_ch_ua;
$data->sec_ch_ua_mobile = $this->isEmpty($data->sec_ch_ua_mobile) ? $this->getHeader('HTTP_SEC_CH_UA_MOBILE') : $data->sec_ch_ua_mobile;
$data->sec_ch_ua_platform = $this->isEmpty($data->sec_ch_ua_platform) ? $this->getHeader('HTTP_SEC_CH_UA_PLATFORM') : $data->sec_ch_ua_platform;
$data->sec_ch_ua_platform_version = $this->isEmpty($data->sec_ch_ua_platform_version) ? $this->getHeader('HTTP_SEC_CH_UA_PLATFORM_VERSION') : $data->sec_ch_ua_platform_version;
$data->sec_ch_width = $this->isEmpty($data->sec_ch_width) ? $this->getHeader('HTTP_SEC_CH_WIDTH') : $data->sec_ch_width;
$data->sec_ch_viewport_width = $this->isEmpty($data->sec_ch_viewport_width) ? $this->getHeader('HTTP_SEC_CH_VIEWPORT_WIDTH') : $data->sec_ch_viewport_width;
$response = $this->client->post(self::SESSION_ENDPOINT, [
'headers' => $this->getRequestHeader(),
'json' => [
'ip' => $this->getHeader('REMOTE_ADDR'),
'user_agent' => $this->getHeader('HTTP_USER_AGENT'),
'sec_ch_ua' => $this->getHeader('HTTP_SEC_CH_UA'),
'sec_ch_ua_mobile' => $this->getHeader('HTTP_SEC_CH_UA_MOBILE'),
'sec_ch_ua_platform' => $this->getHeader('HTTP_SEC_CH_UA_PLATFORM'),
'sec_ch_ua_platform_version' => $this->getHeader('HTTP_SEC_CH_UA_PLATFORM_VERSION'),
'sec_ch_width' => $this->getHeader('HTTP_SEC_CH_WIDTH'),
'sec_ch_viewport_width' => $this->getHeader('HTTP_SEC_CH_VIEWPORT_WIDTH'),
'ip' => $data->ip,
'user_agent' => $data->user_agent,
'sec_ch_ua' => $data->sec_ch_ua,
'sec_ch_ua_mobile' => $data->sec_ch_ua_mobile,
'sec_ch_ua_platform' => $data->sec_ch_ua_platform,
'sec_ch_ua_platform_version' => $data->sec_ch_ua_platform_version,
'sec_ch_width' => $data->sec_ch_width,
'sec_ch_viewport_width' => $data->sec_ch_viewport_width,
]
]);
return json_decode($response->getBody());
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->session(false);
} else if ($e->getResponse()->getStatusCode() != 200) {
} else if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error extending session: '.$e->getResponse()->getBody());
}
}
Expand All @@ -251,10 +266,10 @@ function domain($retry = true) {

return $domains[0];
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->domain(false);
} else if ($e->getResponse()->getStatusCode() != 200) {
} else if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error getting domain: '.$e->getResponse()->getBody());
}
}
Expand Down Expand Up @@ -394,6 +409,14 @@ function keywords(Filter $filter) {
return $this->performGet(self::KEYWORDS_ENDPOINT, $filter);
}

function listFunnel(Filter $filter) {
return $this->performGet(self::LIST_FUNNEL_ENDPOINT, $filter);
}

function funnel(Filter $filter) {
return $this->performGet(self::FUNNEL_ENDPOINT, $filter);
}

private function performGet($url, Filter $filter, $retry = true) {
try {
if ($this->getAccessToken() === '' && $retry) {
Expand All @@ -406,10 +429,10 @@ private function performGet($url, Filter $filter, $retry = true) {
]);
return json_decode($response->getBody());
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() == 401 && $retry) {
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() == 401 && $retry) {
$this->refreshToken();
return $this->performGet($url, $filter, false);
} else if ($e->getResponse()->getStatusCode() != 200) {
} else if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error getting result for '.$url.': '.$e->getResponse()->getBody());
}
}
Expand Down Expand Up @@ -439,8 +462,8 @@ private function refreshToken() {
$resp = json_decode($response->getBody());
$_SESSION['pirsch_access_token'] = $resp->access_token;
} catch(\GuzzleHttp\Exception\RequestException $e) {
if ($e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error refreshing token '.$e->getResponse()->getStatusCode().': '.$e->getResponse()->getBody());
if (!is_null($e->getResponse()) && $e->getResponse()->getStatusCode() != 200) {
throw new \Exception('Error refreshing token '.!is_null($e->getResponse()) && $e->getResponse()->getStatusCode().': '.$e->getResponse()->getBody());
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ class Filter {
public $sort;
public $direction;
public $search;
public $funnel_id;
}
Loading

0 comments on commit 2e3ff4b

Please sign in to comment.