From 5df522f2260c2851a0cb005cde0e942d772496b3 Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Wed, 11 Jun 2014 13:24:03 -0400 Subject: [PATCH 1/4] Added GTM code and data layer --- airtime_mvc/application/Bootstrap.php | 3 +- .../controllers/ShowbuilderController.php | 88 +++++++++++++++++++ .../application/layouts/scripts/layout.phtml | 9 ++ 3 files changed, 99 insertions(+), 1 deletion(-) diff --git a/airtime_mvc/application/Bootstrap.php b/airtime_mvc/application/Bootstrap.php index 64347b8db..2307b8061 100644 --- a/airtime_mvc/application/Bootstrap.php +++ b/airtime_mvc/application/Bootstrap.php @@ -146,9 +146,10 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap } } + /* if (isset($CC_CONFIG['demo']) && $CC_CONFIG['demo'] == 1) { $view->headScript()->appendFile($baseUrl.'js/libs/google-analytics.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); - } + }*/ } protected function _initViewHelpers() diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 7df1bb7ad..66fb33a7f 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -28,6 +28,7 @@ class ShowbuilderController extends Zend_Controller_Action $user = Application_Model_User::GetCurrentUser(); $userType = $user->getType(); $this->view->headScript()->appendScript("localStorage.setItem( 'user-type', '$userType' );"); + $this->view->headScript()->appendScript($this->generateGoogleTagManagerDataLayerJavaScript()); $this->view->headScript()->appendFile($baseUrl.'js/contextmenu/jquery.contextMenu.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); $this->view->headScript()->appendFile($baseUrl.'js/datatables/js/jquery.dataTables.js?'.$CC_CONFIG['airtime_version'],'text/javascript'); @@ -378,4 +379,91 @@ class ShowbuilderController extends Zend_Controller_Action throw new Exception("this controller is/was a no-op please fix your code"); } + + /** Returns a string containing the JavaScript code to pass some billing account info + * into Google Tag Manager / Google Analytics, so we can track things like the plan type. + */ + private static function generateGoogleTagManagerDataLayerJavaScript() + { + $code = ""; + + try + { + $accessKey = $_SERVER["WHMCS_ACCESS_KEY"]; + $username = $_SERVER["WHMCS_USERNAME"]; + $password = $_SERVER["WHMCS_PASSWORD"]; + $url = "https://account.sourcefabric.com/includes/api.php?accesskey=" . $accessKey; # URL to WHMCS API file goes here + + $postfields = array(); + $postfields["username"] = $username; + $postfields["password"] = md5($password); + $postfields["action"] = "getclientsdetails"; + $postfields["stats"] = true; + $postfields["clientid"] = Application_Model_Preference::GetClientId(); + $postfields["responsetype"] = "json"; + + $query_string = ""; + foreach ($postfields AS $k=>$v) $query_string .= "$k=".urlencode($v)."&"; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); //Aggressive 5 second timeout + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $query_string); + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); + $jsondata = curl_exec($ch); + if (curl_error($ch)) { + //die("Connection Error: ".curl_errno($ch).' - '.curl_error($ch)); + throw new Exception("WHMCS server down or invalid request."); + } + curl_close($ch); + + $arr = json_decode($jsondata); # Decode JSON String + + $client = $arr->client; + $stats = $arr->stats; + $currencyCode = $client->currency_code; + $incomeCents = NumberFormatter::parseCurrency($stats->income, $currencyCode); + + $isTrial = true; + if ($incomeCents > 0) { + $isTrial = false; + } + $plan = Application_Model_Preference::GetPlanLevel(); + $country = $client->country; + $postcode = $client->postcode; + + //Figure out how long the customer has been around using a mega hack. + //(I'm avoiding another round trip to WHMCS for now...) + //We calculate it based on the trial end date... + $trialEndDateStr = Application_Model_Preference::GetTrialEndingDate(); + if ($trialEndDateStr == '') { + $accountDuration = 0; + } else { + $trialDurationDays = 30; + $today = new DateTime(); + $trialEndDate = new DateTime($trialEndDate); + $interval = $today->diff($trialEndDate); + $accountDuration = $trialDurationDays - $interval->d; + } + + $code = "dataLayer.push({ + 'ZipCode': '" . $postcode . "', + 'UserID': '" . $client->id . "', + 'Customer': 'Customer', + 'PlanType': '" . $plan . "', + 'Trial': '" . $isTrial . "', + 'Country': '" . $country . "', + 'AccountDuration': '" . $accountDuration . "' + });"; + + } + catch (Exception $e) + { + return ""; + } + return $code; + } } diff --git a/airtime_mvc/application/layouts/scripts/layout.phtml b/airtime_mvc/application/layouts/scripts/layout.phtml index fc46df5c4..a85ede7e6 100644 --- a/airtime_mvc/application/layouts/scripts/layout.phtml +++ b/airtime_mvc/application/layouts/scripts/layout.phtml @@ -9,6 +9,15 @@ + + + + partial('partialviews/trialBox.phtml', array("is_trial"=>$this->isTrial(), "trial_remain"=> $this->trialRemaining())) ?>
From d984ec9b43d79e33e735df3d72eeec78472d412b Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Wed, 11 Jun 2014 13:45:23 -0400 Subject: [PATCH 2/4] Don't use NumberFormatter because of PECL intl dependency --- .../application/controllers/ShowbuilderController.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 66fb33a7f..4e8a1d4fc 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -425,12 +425,16 @@ class ShowbuilderController extends Zend_Controller_Action $client = $arr->client; $stats = $arr->stats; $currencyCode = $client->currency_code; - $incomeCents = NumberFormatter::parseCurrency($stats->income, $currencyCode); + //$incomeCents = NumberFormatter::parseCurrency($stats->income, $currencyCode); $isTrial = true; - if ($incomeCents > 0) { + if (strpos($stats->income, "0.00") === FALSE) { $isTrial = false; } + /* + if ($incomeCents > 0) { + $isTrial = false; + }*/ $plan = Application_Model_Preference::GetPlanLevel(); $country = $client->country; $postcode = $client->postcode; @@ -456,7 +460,7 @@ class ShowbuilderController extends Zend_Controller_Action 'PlanType': '" . $plan . "', 'Trial': '" . $isTrial . "', 'Country': '" . $country . "', - 'AccountDuration': '" . $accountDuration . "' + 'AccountDuration': '" . strval($accountDuration) . "' });"; } From 994dbddac21273829ea895644c52da57d3e0f9bc Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Wed, 11 Jun 2014 13:52:42 -0400 Subject: [PATCH 3/4] Use document ready() event to fire data layer --- .../application/controllers/ShowbuilderController.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 4e8a1d4fc..6a7608097 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -453,7 +453,8 @@ class ShowbuilderController extends Zend_Controller_Action $accountDuration = $trialDurationDays - $interval->d; } - $code = "dataLayer.push({ + $code = "$( document ).ready(function() { + dataLayer.push({ 'ZipCode': '" . $postcode . "', 'UserID': '" . $client->id . "', 'Customer': 'Customer', @@ -461,7 +462,8 @@ class ShowbuilderController extends Zend_Controller_Action 'Trial': '" . $isTrial . "', 'Country': '" . $country . "', 'AccountDuration': '" . strval($accountDuration) . "' - });"; + }); + });"; } catch (Exception $e) From e3850f79dac85d96a85cd267e95fb8188f3941f0 Mon Sep 17 00:00:00 2001 From: Albert Santoni Date: Wed, 11 Jun 2014 14:09:40 -0400 Subject: [PATCH 4/4] Fix for detecting account duration --- .../application/controllers/ShowbuilderController.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/airtime_mvc/application/controllers/ShowbuilderController.php b/airtime_mvc/application/controllers/ShowbuilderController.php index 6a7608097..2ccd29ce4 100644 --- a/airtime_mvc/application/controllers/ShowbuilderController.php +++ b/airtime_mvc/application/controllers/ShowbuilderController.php @@ -446,11 +446,12 @@ class ShowbuilderController extends Zend_Controller_Action if ($trialEndDateStr == '') { $accountDuration = 0; } else { - $trialDurationDays = 30; $today = new DateTime(); - $trialEndDate = new DateTime($trialEndDate); - $interval = $today->diff($trialEndDate); - $accountDuration = $trialDurationDays - $interval->d; + $trialEndDate = new DateTime($trialEndDateStr); + $trialDuration = new DateInterval("P30D"); //30 day trial duration + $accountCreationDate = $trialEndDate->sub($trialDuration); + $interval = $today->diff($accountCreationDate); + $accountDuration = $interval->days; } $code = "$( document ).ready(function() {