-
-
Save involer/ce5a154c7caa4eea5285 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
class Calq { | |
public static function trackSignup($action) { | |
self::trackLogin($action); | |
} | |
public static function trackLogin($action) { | |
try { | |
self::updateUserIdentity(); | |
self::setupUserProfile(); | |
$data = array( | |
'action_name' => (string) $action, | |
'actor' => (string) self::getSessionUserId(), | |
'properties' => (object) null | |
); | |
self::callCalqApi($data, 'track'); | |
} catch (Exception $exc) { | |
self::handleCalqException($exc); | |
} | |
} | |
public static function trackLogout($action) { | |
try { | |
$data = array( | |
'action_name' => (string) $action, | |
'actor' => (string) self::getCookiesUserId(), | |
'properties' => (object) null | |
); | |
self::callCalqApi($data, 'track'); | |
self::deleteCookiesUserId(); | |
} catch (Exception $exc) { | |
self::handleCalqException($exc); | |
} | |
} | |
public static function trackEvent($action, array $properties = array()) { | |
try { | |
if ($action == 'login' || $action == 'signup') { | |
self::updateUserIdentity(); | |
self::setupUserProfile(); | |
$actor = self::getSessionUserId(); | |
} else { | |
$actor = self::getCookiesUserId(); | |
} | |
$data = array( | |
'action_name' => (string) $action, | |
'properties' => (object) $properties, | |
'actor' => (string) $actor | |
); | |
self::callCalqApi($data, 'track'); | |
if ($action == 'logout') { | |
self::deleteCookiesUserId(); | |
} | |
} catch (Exception $exc) { | |
self::handleCalqException($exc); | |
} | |
} | |
protected static function handleCalqException(Exception $exc) { | |
Log::error("Unable to send calq event. " | |
. "Code: " . $exc->getCode() . " " | |
. "Message: " . $exc->getMessage() . " " | |
. "Trace: " . $exc->getTraceAsString()); | |
} | |
protected static function updateUserIdentity() { | |
$data = array( | |
'old_actor' => (string) self::getCookiesUserId(), | |
'new_actor' => (string) self::getSessionUserId() | |
); | |
self::callCalqApi($data, 'transfer'); | |
$calqData = new stdClass(); | |
$calqData->HasTracked = false; | |
$calqData->IsAnon = true; | |
$calqData->actor = self::getSessionUserId(); | |
setcookie('_calq_d', base64_encode(json_encode($calqData)), Config::get('plugins.calq.cookie_expire'), Config::get('plugins.calq.cookie_path'), Config::get('plugins.calq.cookie_domain')); | |
} | |
protected static function setupUserProfile() { | |
$properties = array( | |
'$email' => Auth::user()->email, | |
'$full_name' => Auth::user()->name | |
); | |
$data = array( | |
'actor' => (string) self::getSessionUserId(), | |
'properties' => (object) $properties | |
); | |
self::callCalqApi($data, 'profile'); | |
} | |
protected static function callCalqApi(array $data, $method) { | |
if(App::environment() == 'production') { | |
$data['write_key'] = Config::get('plugins.calq.paddle-vendor.key'); | |
} else { | |
$data['write_key'] = Config::get('plugins.calq.paddle-vendor.staging-key'); | |
} | |
$data['ip_address'] = Request::getClientIp(); | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, Config::get('plugins.calq.url') . '/' . $method); | |
curl_setopt($ch, CURLOPT_POST, 1); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array('data' => json_encode($data)))); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded')); | |
$decodedResponse = json_decode(curl_exec($ch)); | |
$info = curl_getinfo($ch); | |
if ($info['http_code'] !== 200) { | |
throw new Exception($decodedResponse->error, $info['http_code']); | |
} | |
curl_close($ch); | |
} | |
public static function getSessionUserId() { | |
if (Auth::check()) { | |
switch (Config::get('auth.model')) { | |
case 'Vendor': | |
return 'paddle-vendor-' . Auth::user()->id; | |
case 'User': | |
return 'paddle-user-' . Auth::user()->id; | |
} | |
} | |
} | |
public static function getCookiesUserId() { | |
if (!isset($_COOKIE['_calq_d'])) { | |
$calqData = new stdClass(); | |
$calqData->HasTracked = false; | |
$calqData->IsAnon = true; | |
$calqData->actor = md5(uniqid()); | |
setcookie('_calq_d', base64_encode(json_encode($calqData)), Config::get('plugins.calq.cookie_expire'), Config::get('plugins.calq.cookie_path'), Config::get('plugins.calq.cookie_domain')); | |
return $calqData->actor; | |
} else { | |
return json_decode(base64_decode($_COOKIE['_calq_d']))->actor; | |
} | |
} | |
protected static function deleteCookiesUserId() { | |
setcookie('_calq_d', NULL, time() - 3600, Config::get('plugins.calq.cookie_path'), Config::get('plugins.calq.cookie_domain')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment