Skip to content

Commit

Permalink
Fixed issue #14310: Google Analytics functionality doesn't work (#2762)
Browse files Browse the repository at this point in the history
Co-authored-by: lapiudevgit <devgit@lapiu.biz>
  • Loading branch information
gabrieljenik and lapiudevgit committed Dec 7, 2022
1 parent 3432032 commit 8723363
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 31 deletions.
2 changes: 2 additions & 0 deletions application/config/internal.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@
'lightencss' => 'LS_Twig_Extension::lightencss',
'makeFlashMessage' => 'makeFlashMessage',
'getAllTokenAnswers' => 'LS_Twig_Extension::getAllTokenAnswers',
'getGoogleAnalyticsTrackingUrl' => 'LS_Twig_Extension::getGoogleAnalyticsTrackingUrl',
),
'filters' => array(
'jencode' => 'CJSON::encode',
Expand Down Expand Up @@ -431,6 +432,7 @@
'lightencss',
'getAllTokenAnswers',
'makeFlashMessage',
'getGoogleAnalyticsTrackingUrl',
),
),
),
Expand Down
1 change: 1 addition & 0 deletions application/controllers/PrintanswersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public function actionView($surveyid, $printableexport = false)
if (empty($sExportType)) {
Yii::app()->setLanguage($sLanguage);
$aData['aSurveyInfo']['include_content'] = 'printanswers';
$aData['aSurveyInfo']['trackUrlPageName'] = 'printanswers';
Yii::app()->twigRenderer->renderTemplateFromFile('layout_printanswers.twig', $aData, false);
} elseif ($sExportType == 'pdf') {
// Get images for TCPDF from template directory
Expand Down
1 change: 1 addition & 0 deletions application/controllers/StatisticsUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public function actionAction($surveyid, $language = null)

Yii::app()->getClientScript()->registerScriptFile(Yii::app()->getConfig('generalscripts') . 'statistics_user.js');
$data['aSurveyInfo']['include_content'] = 'statistics_user';
$data['aSurveyInfo']['trackUrlPageName'] = 'statistics_user';
// Set template into last instance. Will be picked up later by the renderer
$oTemplate = Template::model()->getInstance('', $iSurveyID);
Yii::app()->twigRenderer->renderTemplateFromFile('layout_statistics_user.twig', $data, false);
Expand Down
1 change: 1 addition & 0 deletions application/controllers/survey/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ public function action()
$thissurvey['aLoadForm'] = $aLoadForm;
//$oTemplate->registerAssets();
$thissurvey['include_content'] = 'load';
$thissurvey['trackUrlPageName'] = 'load';
Yii::app()->twigRenderer->renderTemplateFromFile("layout_global.twig", array('oSurvey' => Survey::model()->findByPk($surveyid), 'aSurveyInfo' => $thissurvey), false);
}

Expand Down
54 changes: 54 additions & 0 deletions application/core/LS_Twig_Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,58 @@ public static function getLanguageRTL($sLanguageCode)
Yii::app()->loadHelper('surveytranslator');
return getLanguageRTL($sLanguageCode);
}

/**
* Returns the "tracking url" for Google Analytics when style is "Survey-SID/GROUP"
* @param int $surveyId
* @param string $trackUrlPageName Specific page name to include in the tracking url. If it's empty, we will try to infer it from the context.
* @return string The tracking URL as "<survey name>-[<survey id>]/[<page name|group seq>]-<group name>"
*/
public static function getGoogleAnalyticsTrackingUrl($surveyId, $trackUrlPageName = '')
{
$survey = Survey::model()->findByPk($surveyId);

$googleAnalyticsAPIKey = $survey->getGoogleanalyticsapikey();
$googleAnalyticsStyle = isset($survey->googleanalyticsstyle) ? $survey->googleanalyticsstyle : '1';

// Tracking URL can only be used if there is an API key and the style is set to "Survey-SID/GROUP"
if ($googleAnalyticsAPIKey == '' || $googleAnalyticsStyle != 2) {
return '';
}

$surveyName = $survey->localizedTitle;
$groupName = '';

// If a page name is specified, use that. Otherwise, try to get it from the context.
if (!empty($trackUrlPageName)) {
$page = $trackUrlPageName;
} else {
$moveInfo = LimeExpressionManager::GetLastMoveResult();
if (is_null($moveInfo) || (isset($moveInfo['at_start']) && $moveInfo['at_start'])) {
$page = 'welcome';
} elseif ($moveInfo['finished']) {
$page = 'finished';
} else {
$showgroupinfo = Yii::app()->getConfig('showgroupinfo');
if ($survey->format == 'A') {
$page = 1;
} else {
if (
$showgroupinfo == 'both'
|| $showgroupinfo == 'name'
|| ($showgroupinfo == 'choose' && !isset($survey->showgroupinfo))
|| ($showgroupinfo == 'choose' && $survey->showgroupinfo == 'B')
|| ($showgroupinfo == 'choose' && $survey->showgroupinfo == 'N')
) {
$groupInfo = LimeExpressionManager::GetStepIndexInfo($moveInfo['seq']);
$groupName = isset($groupInfo['gname']) ? $groupInfo['gname'] : '';
}
$page = $moveInfo['gseq']+1;
};
}
}

$trackURL = htmlspecialchars($surveyName . '-[' . $surveyId . ']/[' . $page . ']-' . $groupName);
return $trackURL;
}
}
1 change: 1 addition & 0 deletions application/helpers/SurveyRuntimeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ public function saveAllIfNeeded()
$this->aSurveyInfo['aSaveForm'] = $cSave->getSaveFormDatas($this->aSurveyInfo['sid']);

$this->aSurveyInfo['include_content'] = 'save';
$this->aSurveyInfo['trackUrlPageName'] = 'save';
Yii::app()->twigRenderer->renderTemplateFromFile("layout_global.twig", array('oSurvey' => Survey::model()->findByPk($this->iSurveyid), 'aSurveyInfo' => $this->aSurveyInfo), false);
} else {
// Intentional retest of all conditions to be true, to make sure we do have tokens and surveyid
Expand Down
2 changes: 2 additions & 0 deletions application/helpers/common_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,7 @@ function getSurveyInfo($surveyid, $languagecode = '', $force = false)
$thissurvey['email_register'] = $thissurvey['surveyls_email_register'];
$thissurvey['attributedescriptions'] = $result->survey->tokenAttributes;
$thissurvey['attributecaptions'] = $result->attributeCaptions;
$thissurvey['googleanalyticsapikey'] = $oSurvey->getGoogleanalyticsapikey();
if (!isset($thissurvey['adminname'])) {
$thissurvey['adminname'] = Yii::app()->getConfig('siteadminemail');
}
Expand All @@ -805,6 +806,7 @@ function getSurveyInfo($surveyid, $languagecode = '', $force = false)
} else {
$thissurvey['owner_username'] = '';
}


$staticSurveyInfo[$surveyid][$languagecode] = $thissurvey;
}
Expand Down
28 changes: 0 additions & 28 deletions application/helpers/replacements_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,34 +254,6 @@ function templatereplace($line, $replacements = array(), &$redata = array(), $de
$_restart = "";
$_return_to_survey = "";

if (isset($thissurvey['googleanalyticsapikey']) && $thissurvey['googleanalyticsapikey'] === "9999useGlobal9999") {
$_googleAnalyticsAPIKey = trim(App()->getConfig('googleanalyticsapikey'));
} elseif (isset($thissurvey['googleanalyticsapikey']) && trim($thissurvey['googleanalyticsapikey']) != '') {
$_googleAnalyticsAPIKey = trim($thissurvey['googleanalyticsapikey']);
} else {
$_googleAnalyticsAPIKey = "";
}

$thissurvey['googleanalyticsapikey'] = $_googleAnalyticsAPIKey;

$_googleAnalyticsStyle = (isset($thissurvey['googleanalyticsstyle']) ? $thissurvey['googleanalyticsstyle'] : '1');

if ($_googleAnalyticsAPIKey != '' && $_googleAnalyticsStyle == 2) {
// SurveyName-[SID]/[GSEQ]-GroupName - create custom GSEQ based upon page step
$moveInfo = LimeExpressionManager::GetLastMoveResult();
if (is_null($moveInfo)) {
$gseq = 'welcome';
} elseif ($moveInfo['finished']) {
$gseq = 'finished';
} elseif (isset($moveInfo['at_start']) && $moveInfo['at_start']) {
$gseq = 'welcome';
} elseif (is_null($_groupname)) {
$gseq = 'printanswers';
} else {
$gseq = $moveInfo['gseq'] + 1;
}
}

$_endtext = '';
if (isset($thissurvey['surveyls_endtext']) && trim($thissurvey['surveyls_endtext']) != '') {
$_endtext = $thissurvey['surveyls_endtext'];
Expand Down
4 changes: 2 additions & 2 deletions application/models/Survey.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,9 +842,9 @@ public function setGoogleanalyticsapikeysetting($value)
public function getGoogleanalyticsapikey()
{
if ($this->googleanalyticsapikey === "9999useGlobal9999") {
return getGlobalSetting('googleanalyticsapikey');
return trim(Yii::app()->getConfig('googleanalyticsapikey'));
} else {
return $this->googleanalyticsapikey;
return trim($this->googleanalyticsapikey);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@
ga('send', 'pageview');
</script>
{% else %}
{% set trackUrl = getGoogleAnalyticsTrackingUrl(aSurveyInfo.sid, aSurveyInfo.trackUrlPageName ?? '') %}
<script>
(function(i,s,o,g,r,a,m){ i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments) }
,i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', '{{ aSurveyInfo.googleanalyticsapikey }}', 'auto');
ga('send', 'pageview');
ga('send', 'pageview', '{{ aSurveyInfo.trackURL }}');
ga('send', 'pageview', '{{ trackUrl }}');
</script>
{% endif %}
{% endif %}

0 comments on commit 8723363

Please sign in to comment.