Implemented server-side analytics tracking for revenue

This commit is contained in:
Hillel Coren 2016-05-08 16:54:16 +03:00
parent 7c67e66fb0
commit 594b0e1bef
6 changed files with 58 additions and 4 deletions

View File

@ -477,6 +477,7 @@ class PaymentController extends BaseController
if ($account->account_key == NINJA_ACCOUNT_KEY) {
Session::flash('trackEventCategory', '/account');
Session::flash('trackEventAction', '/buy_pro_plan');
Session::flash('trackEventAmount', $payment->amount);
}
return Redirect::to('view/'.$payment->invitation->invitation_key);

View File

@ -572,6 +572,7 @@ if (!defined('CONTACT_EMAIL')) {
define('REFERRAL_PROGRAM_URL', env('REFERRAL_PROGRAM_URL', 'https://www.invoiceninja.com/referral-program/'));
define('EMAIL_MARKUP_URL', env('EMAIL_MARKUP_URL', 'https://developers.google.com/gmail/markup'));
define('OFX_HOME_URL', env('OFX_HOME_URL', 'http://www.ofxhome.com/index.php/home/directory/all'));
define('GOOGLE_ANALYITCS_URL', env('GOOGLE_ANALYITCS_URL', 'https://www.google-analytics.com/collect'));
define('BLANK_IMAGE', 'data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=');
@ -585,7 +586,6 @@ if (!defined('CONTACT_EMAIL')) {
define('INVOICE_DESIGNS_AFFILIATE_KEY', 'T3RS74');
define('SELF_HOST_AFFILIATE_KEY', '8S69AD');
define('PRO_PLAN_PRICE', env('PRO_PLAN_PRICE', 50));
define('PLAN_PRICE_PRO_MONTHLY', env('PLAN_PRICE_PRO_MONTHLY', 5));
define('PLAN_PRICE_PRO_YEARLY', env('PLAN_PRICE_PRO_YEARLY', 50));
define('PLAN_PRICE_ENTERPRISE_MONTHLY', env('PLAN_PRICE_ENTERPRISE_MONTHLY', 10));

View File

@ -0,0 +1,54 @@
<?php namespace App\Listeners;
use Log;
use Utils;
use App\Events\PaymentWasCreated;
class AnalyticsListener
{
public function trackRevenue(PaymentWasCreated $event)
{
if ( ! Utils::isNinja() || ! env('ANALYTICS_KEY')) {
return;
}
$payment = $event->payment;
$invoice = $payment->invoice;
$account = $payment->account;
if ($account->account_key != NINJA_ACCOUNT_KEY) {
return;
}
$analyticsId = env('ANALYTICS_KEY');
$client = $payment->client;
$amount = $payment->amount;
$base = "v=1&tid={$analyticsId}&cid{$client->public_id}&cu=USD&ti={$invoice->invoice_number}";
$url = $base . "&t=transaction&ta=ninja&tr={$amount}";
$this->sendAnalytics($url);
//Log::info($url);
$url = $base . "&t=item&in=plan&ip={$amount}&iq=1";
$this->sendAnalytics($url);
//Log::info($url);
}
private function sendAnalytics($data)
{
$data = json_encode($data);
$curl = curl_init();
$opts = [
CURLOPT_URL => GOOGLE_ANALYITCS_URL,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => 'POST',
CURLOPT_POSTFIELDS => $data,
];
curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
curl_close($curl);
}
}

View File

@ -799,7 +799,6 @@ class Invoice extends EntityModel implements BalanceAffecting
$invitation = $this->invitations[0];
$link = $invitation->getLink('view', true);
$key = env('PHANTOMJS_CLOUD_KEY');
$curl = curl_init();
if (Utils::isNinjaDev()) {
$link = env('TEST_LINK');

View File

@ -98,6 +98,7 @@ class EventServiceProvider extends ServiceProvider {
'App\Listeners\SubscriptionListener@createdPayment',
'App\Listeners\InvoiceListener@createdPayment',
'App\Listeners\NotificationListener@createdPayment',
'App\Listeners\AnalyticsListener@trackRevenue',
],
'App\Events\PaymentWasArchived' => [
'App\Listeners\ActivityListener@archivedPayment',

View File

@ -177,9 +177,8 @@
});
@if (Session::has('trackEventCategory') && Session::has('trackEventAction'))
trackEvent('{{ session('trackEventCategory') }}', '{{ session('trackEventAction') }}');
@if (Session::get('trackEventAction') === '/buy_pro_plan')
window._fbq.push(['track', '{{ env('FACEBOOK_PIXEL_BUY_PRO') }}', {'value':'{{ PRO_PLAN_PRICE }}.00','currency':'USD'}]);
window._fbq.push(['track', '{{ env('FACEBOOK_PIXEL_BUY_PRO') }}', {'value':'{{ session('trackEventAmount') }}','currency':'USD'}]);
@endif
@endif