How to add a tracker in your XWiki using JavaScriptExtension object?
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: https://matomo.org/docs/installation/.
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?
- Trackers like Matomo Cloud (Install the JavaScript Tracking Tag) or Google Tag Manager (Quick Start Guide) need to be in added immediately before your closing </head> tag.
- Others like Albacross (tracking code install guide from Albacross docs ) need to be in added immediately before your closing </body> tag of all pages of your website.
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','serve.albacross.com/track.js','__REPLACE_THIS_WITH_YOUR_CLIENT_ID__')";
document.body.appendChild(script);
}); - 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.
- 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:
// Add Global site tag (gtag.js) - Google Analytics
var script = document.createElement('script');
script.type='text/javascript';
script.src='https://www.googletagmanager.com/gtag/js?id=UA-REPLACE_THIS_WITH_YOUR_CLIENT_ID-Y';
script.async = true;
document.head.appendChild(script);
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', 'UA-REPLACE_THIS_WITH_YOUR_CLIENT_ID-Y');
});
- Replace this placeholder REPLACE_THIS_WITH_YOUR_CLIENT_ID with your unique Client ID.
- Finish up by clicking "Save and View".