This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
forked from segmentio/analytics-php
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from leahciMic/fornax
Fornax consumer
- Loading branch information
Showing
8 changed files
with
226 additions
and
38 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
|
||
test: | ||
phpunit --colors test/ | ||
|
||
vendor/phpunit/phpunit/phpunit.php -d date.timezone=Australia/Sydney --colors test/ | ||
|
||
.PHONY: test | ||
.PHONY: test |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
namespace SegmentIO; | ||
|
||
class Analytics_Consumer_Fornax extends Analytics_Consumer { | ||
private $file_handle; | ||
protected $type = "Fornax"; | ||
const LOG_TTL = 300; // 5 minutes | ||
|
||
public function getFilename() | ||
{ | ||
$filename = getmypid() . '_' . (floor(time() / self::LOG_TTL) * self::LOG_TTL) . '.ldjson'; | ||
return $this->options['fornax_base_path'] . $filename; | ||
} | ||
|
||
/** | ||
* The file consumer writes track and identify calls to a file. | ||
* @param string $secret | ||
* @param array $options | ||
* string "filename" - where to log the analytics calls | ||
*/ | ||
public function __construct($secret, $options = array()) { | ||
// default options | ||
$options = array_merge(array( | ||
'filepermissions' => 0777 | ||
), $options); | ||
|
||
parent::__construct($secret, $options); | ||
|
||
$options['filename'] = $this->getFilename(); | ||
|
||
try { | ||
$this->file_handle = fopen($options["filename"], "a"); | ||
chmod($options["filename"], $options["filepermissions"]); | ||
} catch (\Exception $e) { | ||
$this->handleError($e->getCode(), $e->getMessage()); | ||
} | ||
} | ||
|
||
public function __destruct() { | ||
if ($this->file_handle && | ||
get_resource_type($this->file_handle) != "Unknown") { | ||
fclose($this->file_handle); | ||
} | ||
} | ||
|
||
/** | ||
* Tracks a user action | ||
* @param [string] $user_id user id string | ||
* @param [string] $event name of the event | ||
* @param [array] $properties properties associated with the event | ||
* @param [string] $timestamp iso8601 of the timestamp | ||
* @return [boolean] whether the track call succeeded | ||
*/ | ||
public function track($user_id, $event, $properties, $context, $timestamp) { | ||
// Fornax will drop events that do not contain a period | ||
if (!strpos($event, '.')) { | ||
// Append MissingDomain. prefix, so we can group and track these events that do not conform | ||
$event = 'MissingDomain.' . ltrim($event, '.'); | ||
} | ||
|
||
if (isset($this->options['defaultProperties'])) { | ||
$properties = array_merge($properties, $this->options['defaultProperties']); | ||
} | ||
|
||
$body = array( | ||
"userId" => $user_id, | ||
"event" => $event, | ||
"properties" => $properties, | ||
"timestamp" => $timestamp, | ||
"context" => $context, | ||
"action" => "track" | ||
); | ||
|
||
return $this->write($body); | ||
} | ||
|
||
/** | ||
* Tags traits about the user. | ||
* @param [string] $user_id | ||
* @param [array] $traits | ||
* @param [string] $timestamp iso8601 of the timestamp | ||
* @return [boolean] whether the track call succeeded | ||
*/ | ||
public function identify($user_id, $traits, $context, $timestamp) { | ||
|
||
$body = array( | ||
"userId" => $user_id, | ||
"traits" => $traits, | ||
"context" => $context, | ||
"timestamp" => $timestamp, | ||
"action" => "identify" | ||
); | ||
|
||
return $this->write($body); | ||
} | ||
|
||
/** | ||
* Aliases from one user id to another | ||
* @param string $from | ||
* @param string $to | ||
* @param array $context | ||
* @param string $timestamp iso8601 of the timestamp | ||
* @return boolean whether the alias call succeeded | ||
*/ | ||
public function alias($from, $to, $context, $timestamp) { | ||
|
||
$body = array( | ||
"from" => $from, | ||
"to" => $to, | ||
"context" => $context, | ||
"timestamp" => $timestamp, | ||
"action" => "alias" | ||
); | ||
|
||
return $this->write($body); | ||
} | ||
|
||
/** | ||
* Writes the API call to a file as line-delimited json | ||
* @param [array] $body post body content. | ||
* @return [boolean] whether the request succeeded | ||
*/ | ||
private function write($body) { | ||
if (!$this->file_handle) | ||
return false; | ||
|
||
if (!empty($this->options['anonymousId'])) { | ||
$body['anonymousId'] = $this->options['anonymousId']; | ||
} | ||
|
||
$content = json_encode($body); | ||
$content.= "\n"; | ||
|
||
return fwrite($this->file_handle, $content) == strlen($content); | ||
} | ||
} |
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
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
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
Oops, something went wrong.