Skip to content

Instantly share code, notes, and snippets.

@iuli-dercaci
Last active July 20, 2018 19:22
Show Gist options
  • Save iuli-dercaci/549267dbcf75b1ae437403ec2bf6687a to your computer and use it in GitHub Desktop.
Save iuli-dercaci/549267dbcf75b1ae437403ec2bf6687a to your computer and use it in GitHub Desktop.
Babestation Cams GA
<?php
/**
* @param TokenLog $tokenLog
*/
protected function trackCreditTopUpTransaction(TokenLog $tokenLog)
{
$this->debugLogger->addInfo('track credit top up transaction START'); #
$user = $tokenLog->getUser();
$cid = null;
$this->debugLogger->addInfo('track credit top up transaction USER: ' . $user->getId() . ' ' . $user->getUsername()); #
foreach ($user->getAdditionalInformation() as $info) {
/** @var AdditionalInformation $info */
if ($info->getFieldKey() == 'ga_id') {
$cid = $info->getFieldValue();
$this->debugLogger->addInfo('track credit top up USER ga_id: ' . $cid); #
}
}
$token = $tokenLog->getToken();
if ($token == null && !empty($tokenLog->getAmount())) {
$amount = $tokenLog->getAmount();
} elseif ((bool)$token) {
$amount = $token->getPrice();
} else {
return false;
}
$label = $token->getCredits() ." Credits";
//send transaction and then send the item
$postData = [
'v' => 1,
'tid' => $this->siteSettings['googleAnalyticsId'],
'cid' => $cid,
't' => 'transaction',
'ti' => $tokenLog->getTransactionId(),
'ta' => 'topup',
'tr' => $amount,
'cu' => 'GBP'
];
$this->debugLogger->addInfo('track credit top up TRANSACTION POST DATA: ' . $postData); #
$postString = http_build_query($postData);
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, 'https://www.google-analytics.com/collect');
curl_setopt($ch,CURLOPT_POST, count($postData));
curl_setopt($ch,CURLOPT_POSTFIELDS, $postString);
//execute post
$result = curl_exec($ch);
$this->debugLogger->addInfo('track credit top up TRANSACTION CURL RESULT: ' . $result); #
curl_close($ch);
$postData = [
'v' => 1,
'tid' => $this->siteSettings['googleAnalyticsId'],
'cid' => $cid,
't' => 'item',
'ti' => $tokenLog->getTransactionId(),
'in' => $label,
'ip' => $amount,
'iq' => 1,
'cu' => 'GBP'
];
$this->debugLogger->addInfo('track credit top up ITEM POST DATA: ' . $postData); #
$postString = http_build_query($postData);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, 'https://www.google-analytics.com/collect');
curl_setopt($ch,CURLOPT_POST, count($postData));
curl_setopt($ch,CURLOPT_POSTFIELDS, $postString);
//execute post
$result = curl_exec($ch);
$this->debugLogger->addInfo('track credit top up ITEM CURL RESULT: ' . $result); #
//close connection
curl_close($ch);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment