How to add a tracker in your XWiki using JavaScriptExtension object?

Last modified by Thomas Mortagne on 2023/10/10

Examples of trackers that can be added in XWiki

  • Google Analytics . The extension is included by default with the XWiki platform (until XWiki 14.9, after which an Extension needs to be installed) which provides a dedicated section in the wiki preferences page for activating the Google Analytics tracking. You can find more about the extension here.

    All you need to do is to go to Administration > Other > Google Analytics and add you google account.


  • Matomo. It can be installed both on-premise or cloud.

    The installation guide for the on-premise Matomo server is available on their dedicated website:
    The next step, on the XWiki side, is to install the Matomo extension from the wiki Administration section with Extension Manager.

    If you choose to use Matomo cloud services you can integrate in XWiki using JavaScript extension object.

  • Google Tag Manager  The tracker can be added in XWIki by using JavaScript extension object .
  • Albacross . The tracker can be added in XWIki by using JavaScript extension object .

Check out what kind of data is collected by the most popular XWiki Analytics extensions. 

Using a JavaScript extension

The official documentation is a reliable source where you can learn more about JavaScriptExtension Objects and how to use them.

Where should the trackers be injected?

Here are two examples on adding a tracker using a JavaScript extension object.

Example with the Albacross tool

  • The page itself where you add the object can be any XWiki page - an existing page or a new page. Thus, if you prefer the latter, create a new page, e.g. in the MyCode space, called AlbacrossTracker.
  • And edit the page in Object mode. 
  • From the "New Object" drop-down list of the object editor choose JavaScriptExtension. Then, click the "Add" button. Name it "Albacross Tracker", give it a default cache policy, ask it to parse the content, and write the following code:
     document.observe('xwiki:dom:loaded', function() {
     var script = document.createElement('script');
     script.type = 'text/javascript';
     script.innerHTML = "(function(a,l,b,c,r,s){_nQc=c,r=a.createElement(l),s=a.getElementsByTagName(l)[0];r.async=1;r.src=l.src=('https:'==a.location.protocol?'https://':'http://')+b;s.parentNode.insertBefore(r,s);}) (document,'script','','__REPLACE_THIS_WITH_YOUR_CLIENT_ID__')";
  • Don't forget to replace this placeholder __REPLACE_THIS_WITH_YOUR_CLIENT_ID__ with your unique Client ID.
  • Then pick to use the extension "Always on this wiki". For this to happen, however, you need to save the extension document with programming rights. If you are an administrator of the wiki, you might already have programming rights. emoticon_smile
  • To finish, just click "Save and View".

Example with Google Tag Manager tracker

You need to create a JavaScriptExtension Object as explained above on an existing page or a new one. As it needs to be injected immediately before your closing </head> tag, the code will be the following:

document.observe('xwiki:dom:loaded', function() {
 // Add Global site tag (gtag.js) - Google Analytics
 var script = document.createElement('script');
  script.async = true;

  window.dataLayer = window.dataLayer || 
  function gtag() {
js', new Date());

  • Replace this placeholder REPLACE_THIS_WITH_YOUR_CLIENT_ID with your unique Client ID.
  • Finish up by clicking "Save and View".

Get Connected