Created
April 15, 2020 14:41
-
-
Save martin-loetzsch/8375cc55bd9b3886016c11ded7a1505b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'vendor/autoload.php'; | |
// this cookie is set when not present | |
$cookieName = 'visitor'; | |
// retrieve visitor id from cookie | |
$visitorId = array_key_exists($cookieName, $_COOKIE) ? $_COOKIE[$cookieName] : null; | |
if (!$visitorId) { | |
// visitor cookie not set. Use session id as visitor ID | |
$visitorId = sha1(uniqid(s::id(), true)); | |
setcookie($cookieName, $visitorId, | |
time() + (2 * 365 * 24 * 60 * 60), '/', | |
'project-a.com', false, true); | |
} | |
$request = kirby()->request(); | |
// the payload to log | |
$event = [ | |
'visitor_id' => $visitorId, | |
'session_id' => s::id(), | |
'timestamp' => date('c'), | |
'ip' => $request->__debuginfo()['ip'], | |
'url' => $request->url(), | |
'host' => $_SERVER['SERVER_NAME'], | |
'path' => $request->path()->toArray(), | |
'query' => $request->query()->toArray(), | |
'referrer' => visitor::referer(), | |
'language' => visitor::acceptedLanguageCode(), | |
'ua' => visitor::userAgent() | |
]; | |
$firehoseClient = new \Aws\Firehose\FirehoseClient([ | |
// secrets | |
]); | |
// asynchronically publish message to firehose delivery stream | |
$promise = $firehoseClient->putRecordAsync([ | |
'DeliveryStreamName' => 'kinesis-firehose-stream-123', | |
'Record' => ['Data' => json_encode($event)] | |
]); | |
register_shutdown_function(function () use ($promise) { | |
$promise->wait(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment