aioseop_google_analytics

The All In One SEO Pack aioseop google analytics class.

Defined (1)

The class is defined in the following location(s).

/public/google-analytics.php  
  1. class aioseop_google_analytics extends All_in_One_SEO_Pack_Module { 
  2. // TODO Rather than extending the module base class, we should find a better way for the shared functions like moving them to our common functions class. 
  3.  
  4. private $aiosp_ga_use_universal_analytics = true; 
  5.  
  6. function __construct() { 
  7.  
  8. $this->filter_universal(); 
  9.  
  10. $this->google_analytics(); 
  11.  
  12.  
  13. function filter_universal() { 
  14. $aiosp_ga_use_universal_analytics = $this->aiosp_ga_use_universal_analytics; 
  15. $this->aiosp_ga_use_universal_analytics = apply_filters( 'aiosp_universal_analytics', $aiosp_ga_use_universal_analytics ); 
  16.  
  17. function google_analytics() { 
  18. global $aioseop_options; 
  19. $analytics = ''; 
  20. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_exclude_users'] ) && is_user_logged_in() ) { 
  21. global $current_user; 
  22. if ( empty( $current_user ) ) { 
  23. wp_get_current_user(); 
  24. if ( ! empty( $current_user ) ) { 
  25. $intersect = array_intersect( $aioseop_options['aiosp_ga_exclude_users'], $current_user->roles ); 
  26. if ( ! empty( $intersect ) ) { 
  27. return; 
  28. if ( ! empty( $aioseop_options['aiosp_google_analytics_id'] ) ) { 
  29. ob_start(); 
  30. $analytics = $this->universal_analytics(); 
  31. echo $analytics; 
  32. if ( empty( $analytics ) ) { 
  33. ?> 
  34. <script type="text/javascript"> 
  35. var _gaq = _gaq || []; 
  36. <?php if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_link_attribution'] ) ) { 
  37. ?> var pluginUrl = 
  38. '//www.google-analytics.com/plugins/ga/inpage_linkid.js'; 
  39. _gaq.push(['_require', 'inpage_linkid', pluginUrl]); 
  40. <?php 
  41. ?> _gaq.push(['_setAccount', '<?php 
  42. echo $aioseop_options['aiosp_google_analytics_id']; 
  43. ?>']); 
  44. <?php if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_anonymize_ip'] ) ) { 
  45. ?> _gaq.push(['_gat._anonymizeIp']); 
  46. <?php 
  47. ?> 
  48. <?php if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_multi_domain'] ) ) { 
  49. ?> _gaq.push(['_setAllowLinker', true]); 
  50. <?php 
  51. ?> 
  52. <?php if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_domain'] ) ) { 
  53. $domain = $this->get_analytics_domain(); 
  54. ?> _gaq.push(['_setDomainName', '<?php echo $domain; ?>']); 
  55. <?php 
  56. ?> _gaq.push(['_trackPageview']); 
  57. (function () { 
  58. var ga = document.createElement('script'); 
  59. ga.type = 'text/javascript'; 
  60. ga.async = true; 
  61. <?php 
  62. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_display_advertising'] ) ) { 
  63. ?> ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'; 
  64. <?php 
  65. } else { 
  66. ?> ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
  67. <?php 
  68. ?> var s = document.getElementsByTagName('script')[0]; 
  69. s.parentNode.insertBefore(ga, s); 
  70. })(); 
  71. </script> 
  72. <?php 
  73. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && $aioseop_options['aiosp_ga_track_outbound_links'] ) { ?> 
  74. <script type="text/javascript"> 
  75. function recordOutboundLink(link, category, action) { 
  76. <?php if ( $this->aiosp_ga_use_universal_analytics ) { ?> 
  77. ga('send', 'event', category, action); 
  78. <?php } 
  79. if ( ! $this->aiosp_ga_use_universal_analytics ) { ?> 
  80. _gat._getTrackerByName()._trackEvent(category, action); 
  81. <?php } ?> 
  82. if (link.target == '_blank') return true; 
  83. setTimeout('document.location = "' + link.href + '"', 100); 
  84. return false; 
  85. /* use regular Javascript for this */ 
  86. function getAttr(ele, attr) { 
  87. var result = (ele.getAttribute && ele.getAttribute(attr)) || null; 
  88. if (!result) { 
  89. var attrs = ele.attributes; 
  90. var length = attrs.length; 
  91. for (var i = 0; i < length; i++) 
  92. if (attr[i].nodeName === attr) result = attr[i].nodeValue; 
  93. return result; 
  94.  
  95. function aiosp_addLoadEvent(func) { 
  96. var oldonload = window.onload; 
  97. if (typeof window.onload != 'function') { 
  98. window.onload = func; 
  99. } else { 
  100. window.onload = function () { 
  101. if (oldonload) { 
  102. oldonload(); 
  103. func(); 
  104.  
  105. function aiosp_addEvent(element, evnt, funct) { 
  106. if (element.attachEvent) 
  107. return element.attachEvent('on' + evnt, funct); 
  108. else 
  109. return element.addEventListener(evnt, funct, false); 
  110.  
  111. aiosp_addLoadEvent(function () { 
  112. var links = document.getElementsByTagName('a'); 
  113. for (var x = 0; x < links.length; x++) { 
  114. if (typeof links[x] == 'undefined') continue; 
  115. aiosp_addEvent(links[x], 'onclick', function () { 
  116. var mydomain = new RegExp(document.domain, 'i'); 
  117. href = getAttr(this, 'href'); 
  118. if (href && href.toLowerCase().indexOf('http') === 0 && !mydomain.test(href)) { 
  119. recordOutboundLink(this, 'Outbound Links', href); 
  120. }); 
  121. }); 
  122. </script> 
  123. <?php 
  124. $analytics = ob_get_clean(); 
  125. echo apply_filters( 'aiosp_google_analytics', $analytics ); 
  126. do_action( 'after_aiosp_google_analytics' ); 
  127.  
  128.  
  129. function universal_analytics() { 
  130. global $aioseop_options; 
  131. $analytics = ''; 
  132. if ( $this->aiosp_ga_use_universal_analytics ) { 
  133. $allow_linker = $cookie_domain = $domain = $addl_domains = $domain_list = ''; 
  134. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) ) { 
  135. $cookie_domain = $this->get_analytics_domain(); 
  136. if ( ! empty( $cookie_domain ) ) { 
  137. $cookie_domain = esc_js( $cookie_domain ); 
  138. $cookie_domain = "'cookieDomain': '{$cookie_domain}'"; 
  139. if ( empty( $cookie_domain ) ) { 
  140. $domain = ", 'auto'"; 
  141. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_multi_domain'] ) ) { 
  142. $allow_linker = "'allowLinker': true"; 
  143. if ( ! empty( $aioseop_options['aiosp_ga_addl_domains'] ) ) { 
  144. $addl_domains = trim( $aioseop_options['aiosp_ga_addl_domains'] ); 
  145. $addl_domains = preg_split( '/[\s, ]+/', $addl_domains ); 
  146. if ( ! empty( $addl_domains ) ) { 
  147. foreach ( $addl_domains as $d ) { 
  148. $d = $this->sanitize_domain( $d ); 
  149. if ( ! empty( $d ) ) { 
  150. if ( ! empty( $domain_list ) ) { 
  151. $domain_list .= ', '; 
  152. $domain_list .= "'" . $d . "'"; 
  153. $extra_options = ''; 
  154. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_display_advertising'] ) ) { 
  155. $extra_options .= "ga('require', 'displayfeatures');"; 
  156. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_enhanced_ecommerce'] ) ) { 
  157. if ( ! empty( $extra_options ) ) { 
  158. $extra_options .= "\n\t\t\t"; 
  159. $extra_options .= "ga('require', 'ec');"; 
  160. if ( ! empty( $domain_list ) ) { 
  161. if ( ! empty( $extra_options ) ) { 
  162. $extra_options .= "\n\t\t\t"; 
  163. $extra_options .= "ga('require', 'linker');\n\t\t\tga('linker:autoLink', [{$domain_list}] );"; 
  164. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_link_attribution'] ) ) { 
  165. if ( ! empty( $extra_options ) ) { 
  166. $extra_options .= "\n\t\t\t"; 
  167. $extra_options .= "ga('require', 'linkid', 'linkid.js');"; 
  168.  
  169. if ( ! empty( $aioseop_options['aiosp_ga_advanced_options'] ) && ! empty( $aioseop_options['aiosp_ga_anonymize_ip'] ) ) { 
  170. if ( ! empty( $extra_options ) ) { 
  171. $extra_options .= "\n\t\t\t"; 
  172. $extra_options .= "ga('set', 'anonymizeIp', true);"; 
  173. $js_options = array(); 
  174. foreach ( array( 'cookie_domain', 'allow_linker' ) as $opts ) { 
  175. if ( ! empty( $$opts ) ) { 
  176. $js_options[] = $$opts; 
  177. if ( ! empty( $js_options ) ) { 
  178. $js_options = implode( ', ', $js_options ); 
  179. $js_options = ', { ' . $js_options . ' } '; 
  180. } else { 
  181. $js_options = ''; 
  182. $analytics_id = esc_js( $aioseop_options['aiosp_google_analytics_id'] ); 
  183. $analytics = <<<EOF 
  184. <script> 
  185. (function(i, s, o, g, r, a, m) {i['GoogleAnalyticsObject']=r;i[r]=i[r]||function() { 
  186. (i[r].q=i[r].q||[]).push(arguments)}, i[r].l=1*new Date();a=s.createElement(o),  
  187. m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a, m) 
  188. })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); 
  189.  
  190. ga('create', '{$analytics_id}'{$domain}{$js_options}); 
  191. {$extra_options} 
  192. ga('send', 'pageview'); 
  193. </script> 
  194.  
  195. EOF; 
  196.  
  197. return $analytics; 
  198.  
  199. /** 
  200. * @return mixed|string 
  201. */ 
  202. function get_analytics_domain() { 
  203. global $aioseop_options; 
  204. if ( ! empty( $aioseop_options['aiosp_ga_domain'] ) ) { 
  205. return $this->sanitize_domain( $aioseop_options['aiosp_ga_domain'] ); 
  206.  
  207. return ''; 
  208.