| Current Path : /var/www/html/plugins/system/GoogleAnalytics/ |
| Current File : /var/www/html/plugins/system/GoogleAnalytics/GoogleAnalytics.php |
<?php
/**
* @version $version 4.6.4 Peter Bui $
* @copyright Copyright (C) 2012 PB Web Development. All rights reserved.
* @license GNU/GPL, see LICENSE.php
* Updated 29th May 2017
*
* Twitter: @astroboysoup
* Blog: http://pbwebdev.com/blog/
* Email: peter@pbwebdev.com.au
* Website: http://nichextensions.com
*
* Joomla! is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
* DirectMonster by Lunametrics
* MIT License - https://github.com/lunametrics/directmonster
*
*/
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport('joomla.plugin.plugin');
class plgSystemGoogleAnalytics extends JPlugin
{
//initialisation function
function plgGoogleAnalytics(&$subject, $config)
{
parent::__construct($subject, $config);
$this->_plugin = JPluginHelper::getPlugin('system', 'GoogleAnalytics');
$this->_params = new JParameter($this->_plugin->params);
}
//rendering of the output
function onAfterRender()
{
//declare output variables
$verifyOutput = '';
$javascript = '';
$remarketingOutput = '';
$useridOutput = '';
$enhancedOutput = '';
$userid = '';
$trackerCode = '';
$domain = '';
$buffer = '';
$ipTrackingOutput = '';
$forceSSLOutput = '';
$adwordsConversionOutput ='';
// Initialise paramater variables
$type = $this->params->get('type', '');
$trackerCode = $this->params->get('code', '');
$domain = $this->params->get('domain', '');
$placement = $this->params->get('placement', '');
$verify = $this->params->get('verify', '');
$directmonster = $this->params->get('directmonster', '');
$remarketing = $this->params->get('remarketing', '');
$enhanced = $this->params->get('enhanced', '');
$userid = $this->params->get('userid', '');
$adwordsConversionURL = $this->params->get('adwordsConversionURL', '');
$adwordsConversionCode = $this->params->get('adwordsConversionCode', '');
$ecommerceConversionURL = $this->params->get('ecommerceConversionURL', '');
$ecommerceConversionCode = $this->params->get('ecommerceConversionCode', '');
$forceSSL = $this->params->get('forceSSL', '');
$ipTracking = $this->params->get('ipTracking', '');
$multiSub = $this->params->get('multiSub', '');
$multiTop = $this->params->get('multiTop', '');
$sampleRate = $this->params->get('sampleRate', '');
$setCookieTimeout = $this->params->get('setCookieTimeout', '');
$siteSpeedSampleRate = $this->params->get('siteSpeedSampleRate', '');
$visitorCookieTimeout = $this->params->get('visitorCookieTimeout', '');
$app = JFactory::getApplication();
// skip if admin page
if ($app->isAdmin()) {
return;
}
//getting body code and storing as buffer
$buffer = JResponse::getBody();
//Google Webmaster verification
if($verify){
$verifyOutput = '<meta name="google-site-verification" content="' . $verify . '" />';
}
if ($type == 'asynchronous') {
if($directmonster){
$javascript .= '<script src="/plugins/system/GoogleAnalytics/vendor/directmonster1.3.3.js" type="text/javascript"></script>
';
}
// Demographics and Remarketing display advertising.
if ($remarketing)
{
//remarketing enabled code
$remarketingOutput = "ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';\n";
} else {
// standard Google tracking code
$remarketingOutput = "ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n";
}
if ($enhanced){
$enhancedOutput = "var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);";
}
//embed Google Analytics code
$javascript .= "<script type=\"text/javascript\">";
if ($enhancedOutput) {
$javascript .= $enhancedOutput;
}
$javascript .= " var _gaq = _gaq || [];\n _gaq.push(['_setAccount', '" . $trackerCode . "']);\n";
if ($ipTracking) {
$javascript .= " _gaq.push(['_gat._anonymizeIp']);\n"; }
if ($multiSub || $multiTop) {
$javascript .= " _gaq.push(['_setDomainName', '" . $_SERVER['SERVER_NAME'] . "']);\n"; }
if ($multiTop) {
$javascript .= " _gaq.push(['_setAllowLinker', true]);\n"; }
if ($sampleRate) {
$javascript .= " _gaq.push(['_setSampleRate', '" . $sampleRate . "']);\n"; }
if ($setCookieTimeout) {
$javascript .= " _gaq.push(['_setSessionCookieTimeout', '" . $setCookieTimeout . "']);\n"; }
if ($siteSpeedSampleRate) {
$javascript .= " _gaq.push(['_setSiteSpeedSampleRate', '" . $siteSpeedSampleRate . "']);\n"; }
if ($visitorCookieTimeout) {
$javascript .= " _gaq.push(['_setVisitorCookieTimeout', '" . $visitorCookieTimeout . "']);\n"; }
$javascript .= "_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ";
if ($remarketingOutput) {
$javascript .= $remarketingOutput ; }
$javascript .= " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
<!-- Asynchronous Google Analytics PRO 4.6.3 Plugin by PB Web Development -->";
}
//
// UNIVERSAL GOOGLE ANALYTICS CODE OUTPUT
//
if ($type == 'universal') {
if($directmonster){
$javascript .= '<script src="/plugins/system/GoogleAnalytics/vendor/directmonster2.0.2.js" type="text/javascript"></script> ';
}
// Demographics and Remarketing display advertising.
if ($remarketing)
{
$remarketingOutput = "\n ga('require', 'displayfeatures');"; }
if ($enhanced){
$enhancedOutput = "\n ga('require', 'linkid', 'linkid.js');"; }
$jUser = JFactory::getUser();
$jUserID = $jUser->get( 'id' );
if ($userid && $jUserID!=0){
$useridOutput ="\n ga('set', 'userId', ".$jUserID.";";
}
if ($ipTracking){
$ipTrackingOutput = "\n ga('set', 'anonymizeIp', true);";}
if ($forceSSL){
$forceSSLOutput = "\n ga('set', 'forceSSL', true);";}
// final formatted output
$javascript .= "
<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', '" . $trackerCode . "', '" . $domain ."');";
if ($useridOutput) {
$javascript .= $useridOutput; }
if ($enhancedOutput) {
$javascript .= $enhancedOutput; }
if ($remarketingOutput) {
$javascript .= $remarketingOutput; }
if ($ipTrackingOutput) {
$javascript .= $ipTrackingOutput; }
if ($forceSSLOutput) {
$javascript .= $forceSSLOutput; }
$javascript .= "
ga('send', 'pageview');
</script>
<!-- Universal Google Analytics PRO 4.6.3 Plugin by PB Web Development -->\n";
}
if($verifyOutput)
$buffer = preg_replace("/<\/head>/", "\n\n" . $verifyOutput . "\n\n" . "</head>", $buffer);
if($placement=='body'){
// adding the Google Analytics code in the header before the ending </head> tag and then replacing the buffer
$buffer = preg_replace("/<\/body>/", "\n\n" . $javascript . "\n\n</body>", $buffer);
} else {
// adding the Google Analytics code in the header before the ending </head> tag and then replacing the buffer
$buffer = preg_replace("/<\/head>/", "\n\n" . $javascript . "\n\n</head>", $buffer);
}
//Google conversion tracking addition
if($adwordsConversionURL==(JURI::current())){
$adwordsConversionOutput = $adwordsConversionCode;
$buffer = preg_replace("/<\/body>/", "\n" . $adwordsConversionOutput . "\n</body>", $buffer);
}
//Google ecommerce conversion tracking addition
if($ecommerceConversionURL==(JURI::current())){
$ecommerceConversionOutput = $ecommerceConversionCode;
$buffer = preg_replace("/<\/body>/", "\n" . $ecommerceConversionOutput . "\n</body>", $buffer);
}
//output the buffer
JResponse::setBody($buffer);
return true;
}
}
?>