Source of file GoogleAnalyticsControllerExtension.php
Size: 2,727 Bytes - Last Modified: 2021-12-23T10:36:30+00:00
/var/www/docs.ssmods.com/process/src/code/extensions/GoogleAnalyticsControllerExtension.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 | <?php /** * Created by Nivanka Fonseka (nivanka@silverstripers.com). * User: nivankafonseka * Date: 2/3/15 * Time: 4:31 PM * To change this template use File | Settings | File Templates. */ class GoogleAnalyticsControllerExtension extends Extension { function onAfterInit(){ if(GoogleAnalyticsConfigExtension::CanTrackEvents($this->owner)){ $this->IncludeGATrackingCode(); $this->IncludeTrackingEvents(); } } function IncludeGATrackingCode(){ $strCurrentDomain = str_replace(Director::protocol(), '', Director::protocolAndHost()); $strID = SiteConfig::current_site_config()->GoogleAnalyticsTrackingID; $strCode = <<<JS var _gaq = _gaq || []; _gaq.push(['_setAccount', '{$strID}']); _gaq.push(['_setDomainName', '{$strCurrentDomain}']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); JS; if(SiteConfig::current_site_config()->GoogleAnalyticsPosition == 'Head'){ Requirements::insertHeadTags('<script>' . $strCode . '</script>', 'GA_TRACKING_CODE'); } else{ Requirements::customScript($strCode, 'GA_TRACKING_CODE'); } } function IncludeTrackingEvents(){ $page = $this->owner->data(); $events = SiteConfig::current_site_config()->GoogleTrackEvents()->where('EXISTS ( SELECT 1 FROM GoogleTrackEvent_Pages gep WHERE gep.GoogleTrackEventID = GoogleTrackEvent.ID AND gep.SiteTreeID = ' . $page->ID . ' LIMIT 1 )'); if($events->count()){ $strCode = ""; Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js'); Requirements::javascript(THIRDPARTY_DIR . '/jquery-livequery/jquery.livequery.js'); foreach($events as $event){ $strEvent = 'click'; if($event->EventType == 'Hover'){ $strEvent = 'mouseenter'; } $strCategory = addslashes($event->Category); $strAction = addslashes($event->Action); $strLabel = addslashes($event->Label); $strCode.= <<<JS (function(){ $("{$event->Target}").livequery('{$strEvent}', function(){ if(typeof _gaq !== 'undefined'){ _gaq.push(['_trackEvent', '{$strCategory}', '{$strAction}', '{$strLabel}']); } }); })(jQuery); JS; } if(SiteConfig::current_site_config()->GoogleAnalyticsPosition == 'Head'){ Requirements::insertHeadTags('<script>' . $strCode . '</script>', 'GA_TRACKERS'); } else{ Requirements::customScript($strCode, 'GA_TRACKERS'); } } } } |