Google Analytics View Helper
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 117 118 119 120121 122 123 124 125126 127 128 129 130131 132 133 134 135136 137 138 139 140141 142 143 144 145146 147 148 149 150151 152 153 154 155156 157 158 159 160161 162 163 164 165166 167 168 169 170171 172 173 174 175176 177 178 179 180181 182 183 184 185186 187 188 189 190191 192 193 194 195196 197 198 199 200201 202 203 204 205206 207 208 209 210211 212 213 214 215216 217 218 219 220221 222 223 224 225226 227 228 229 230231 232 233 234 235236 237 238 239 240241 242 243 | ## Usage <?php // in layout or init of controller $trackerId = '123'; $googleAnalytics = $this->GoogleAnalytics($trackerId, array( array('setVar', 'sex:f'), array('setVar', 'member_id:' . $memberId), )); // from controller or view $this->GoogleAnalytics()->setVar($trackerId, 'another_var'); $this->view->GoogleAnalytics()->setVar($trackerId, 'online:1'); ?><?php // displaying tracker echo $this->GoogleAnalytics(); ?> ## Helper <?php /** * GoogleAnalytics.php * * See http://www.scribd.com/doc/2261328/InstallingGATrackingCode * * @category BaseZF * @package BaseZF_Framwork * @copyright Copyright (c) 2008 BaseZF * @author Harold Thétiot (hthetiot) */ class View_Helper_GoogleAnalytics{ /** * Tracker options instance */ static protected $_trackerOptionsByIds = array(); /** * Available Trackers options */ static protected $_availableOptions = array ( // Standard Options 'trackPageview', 'setVar', // ECommerce Options 'addItem', 'addTrans', 'trackTrans', // Tracking Options 'setClientInfo', 'setAllowHash', 'setDetectFlash', 'setDetectTitle', 'setSessionTimeOut', 'setCookieTimeOut', 'setDomainName', 'setAllowLinker', 'setAllowAnchor', // Campaign Options 'setCampNameKey', 'setCampMediumKey', 'setCampSourceKey', 'setCampTermKey', 'setCampContentKey', 'setCampIdKey', 'setCampNoKey', // Other 'addOrganic', 'addIgnoredOrganic', 'addIgnoredRef', 'setSampleRate', ); /** * * @param string $trackerId the google analytics tracker id * @param array * * @return $this for more fluent interface */ public function GoogleAnalytics($trackerId = null, array $options = array()) { if (!is_null($trackerId)) { $this->trackPageview($trackerId); if (!empty($options)) { $this->addTrackerOptions($trackerId, $options); } } return $this; } /** * Alias to _addTrackerOption * * @param string $optionsName * @param array $optionsArgs * * @return $this for more fluent interface */ public function __call($optionsName, $optionsArgs) { if (in_array($optionsName, self::$_availableOptions) === false) { throw new Exception('Unknown "' . $optionFunc . '" GoogleAnalytics options'); } if (empty($optionsArgs)) { throw new Exception('Missing TrackerId has first Argument on "$this->GoogleAnalytics->' . $optionFunc . '()" function call'); } $trackerId = array_shift($optionsArgs); $this->_addTrackerOption($trackerId, $optionsName, $optionsArgs); return $this; } /** * Add options from array * * @param string $trackerId the google analytics tracker id * @param array of array option with first value has option name * * @return $this for more fluent interface */ public function addTrackerOptions($trackerId, array $options) { foreach ($options as $optionsArgs) { $optionsName = array_shift($optionsArgs); $this->_addTrackerOption($trackerId, $optionsName, $optionsArgs); } return $this; } /** * Add a tracker option * * @param string $trackerId the google analytics tracker id * @param string $optionsName option name * @param array $optionsArgs option arguments * * @return $this for more fluent interface */ protected function _addTrackerOption($trackerId, $optionsName, array $optionsArgs = array()) { $trackerOptions = &$this->_getTrackerOptions($trackerId); array_unshift($optionsArgs, $optionsName); $trackerOptions[] = $optionsArgs; return $this; } /** * Get tracker's options by tracker id * * @param string $trackerId the google analytics tracker id * * @return array an array of options for requested tracker id */ protected function &_getTrackerOptions($trackerId) { if (!isset(self::$_trackerOptionsByIds[$trackerId])) { self::$_trackerOptionsByIds[$trackerId] = array(); } return self::$_trackerOptionsByIds[$trackerId]; } // // Render // /** * Cast to string representation * * @return string */ public function __toString() { return $this->toString(); } /** * Rendering Google Anaytics Tracker script */ public function toString() { $xhtml = array(); $xhtml[] = '<script type="text/javascript">'; $xhtml[] = 'var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");'; $xhtml[] = 'document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));'; $xhtml[] = '</script>'; $xhtml[] = '<script type="text/javascript">'; $xhtml[] = 'try {'; $i = 0; foreach (self::$_trackerOptionsByIds as $trackerId => $options) { // build tracker name $trackerInstance = 'pageTracker' . ($i > 0 ? $i : null); // init tracker $xhtml[] = 'var ' . $trackerInstance . ' = _gat._getTracker("' . $trackerId . '");'; // add options foreach ($options as $optionsData) { // build tracker func call $optionName = '_' . array_shift($optionsData); // escape options arg $optionArgs = array(); foreach ($optionsData as $arg) { $optionArgs[] = is_numeric($arg) ? $arg : '"' . addslashes($arg) . '"'; } // add options $xhtml[] = $trackerInstance . '.' . $optionName . '(' . implode(',', $optionArgs) . ');'; } $i++; } $xhtml[] = '} catch(err) {}</script>'; $xhtml[] = '</script>'; return implode("n", $xhtml); } } |
Comments
Typo in your exception: 'Unknow' => 'Unknown' in the __call() function.
Great view helper! I was planning on writing one with all of these options, but now I don't have to. Thanks :)
Missing
if ('production' !== APPLICATION_ENV) {
return '';
}
and you have double closed script tags.
Nice helper!
It would be nice to have async version http://googlecode.blogspot.com/2009/12/google-analytics-launches-asynchronous.html
Are there any plans?
You must login before commenting on a snippet. If you do not have an account, please register.
Snippet description
A better Google Analytics Helper support multiple tracker Id and all knows google analytics options.
* Standard Options
trackPageview, setVar
* ECommerce Options
addItem, addTrans, trackTrans
* Tracking Options
setClientInfo, setAllowHash, setDetectFlash, setDetectTitle, setSessionTimeOut, setCookieTimeOut, setDomainName, setAllowLinker, setAllowAnchor
* Campaign Options
setCampNameKey, setCampMediumKey, setCampSourceKey, setCampTermKey, setCampContentKey, setCampIdKey, setCampNoKey
* Other
addOrganic, addIgnoredOrganic, addIgnoredRef, setSampleRate
Snippet details
- Created:
-
Harold Thétiot
- Edited:
-
Harold Thétiot
- Revision Id:
- 77
- Edit Message:
- Initial Release
- Tags:
- google analytics helper
- Comments:
- 5
- Views:
- 569
- Points:
- 2 (2 votes)
1 year ago
Cool, didn't even know about the e-commerce options available in analytics, will have to look into it.