Topic on Skin talk:Chameleon

Legaulph (talkcontribs)

I'm not getting page events with google analytics now. I need to be able to add the code to the page headers.

<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', 'UA-xxxxxxxx, 'auto');
  ga('send', 'pageview');

</script>
 

How can I do this in the chameleon skin?

F.trott (talkcontribs)

The layout of the page elements (nav bar, logo, search bar, etc.) is defined in an XML file. There are several layouts available. Have a look at what's contained in .../skins/chameleon/layouts/. To add your script, the best way would be to adapt the layout file.

  • Copy the layout file you would like to use to a location where you keep your customization stuff. This could even be the root of your MW installation, but I'd recommend a dedicated folder.
  • Edit it and add an HTML type component just before the closing </structure> tag:
<component type="Html"><![CDATA[
<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', 'UA-xxxxxxxx, 'auto');
  ga('send', 'pageview');

</script>
]]></component>
  • Use your custom layout by setting the variable $egChameleonLayoutFile in LocalSettings.php: $egChameleonLayoutFile= __DIR__ . '/path-to-layout-file/standard.xml';

This should probably go into a FAQ somewhere, so please tell me if it worked.

Legaulph (talkcontribs)

I added the code to navhead.xml creating my own version in our sandbox, I was able to see the code was added viewing source. moved it to my dev server with from what I can tell the exact configuration and I do not see it.

The server for the sandbox is SUSE MediaWiki 1.23.6 PHP 5.3.8 (apache2handler) MySQL 5.0.96

The dev server Redhat MediaWiki 1.23.6 PHP 5.3.3 (apache2handler) MySQL 5.6.19-log

Stefahn (talkcontribs)
F.trott (talkcontribs)

I think it should also be possible to add your script using the MediaWiki:Common.js page. If not I'd consider it a bug. See Manual:Interface/JavaScript.

Legaulph (talkcontribs)

I found this snippet that I believe is working

$wgExtensionFunctions[] = 'wfGoogleAnalytics';
function wfGoogleAnalytics() {
  global $wgOut;
  $wgOut->addScript(
'<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", "UA-xxxxxxxx-x", "auto");
  ga("send", "pageview");

</script>'
  );
}
Legaulph (talkcontribs)

I was still not getting behavior information I modified the Google Analytics integration extension that I was using. Modified to look like

/*	return <<<HTML
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("{$wgGoogleAnalyticsAccount}");
pageTracker._trackPageview();
} catch(err) {}
</script>
HTML;
*/
return <<<HTML
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', "$wgGoogleAnalyticsAccount"]);
  _gaq.push(['_setDomainName', 'xxxxxxx']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_setSiteSpeedSampleRate', 50]);
  _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);
  })();

</script>
HTML;
}

F.trott (talkcontribs)

I do not know that extension. Maybe you could try to just add that snippet above ($wgExtensionFunctions[] = 'wfGoogleAnalytics'; function wfGoogleAnalytics() {...}) to your LocalSettings.php. Also, it could help to know if this works with other skins, like vector or monobook.

Legaulph (talkcontribs)

I will test the combination of the 2 scripts in Local settings.
I'm not sure how to get it into the header.
the above 2 setups end up at the bottom of the body. $wgExtensionFunctions[] = 'wfGoogleAnalytics'; function wfGoogleAnalytics() {...}
Will work in local settings

Reply to "Google Analytics"