Integrating Analytics - ΩJr. Publizjr Creates Web Pages

This information lives on a webpage hosted at the following web address: 'https://omegajunior.globat.com/code/publizjr/'.

19 Mar. 2012
A.E.Veltstra

We like to see who visits our webpages. We like to see where they go, and whether or not we can make a sale, following their visits. To do so, regular server-side statistics may prove inadequate. Though they clearly show every request and answer, they rarely offer conversion funnels and behavioral analysis.

Google Analytics is one free service that does provide that functionality. As a limitation, they only catch those sources where visitors allow javascript to be active. The amount of downloads of a movie, for instance, is easier to glean from server-side statistics. To each their own.

Here's how to add Google Analytics to your Publizjr templates. Other services probably allow for a similar approach. Any capable PHP programmer can use these instructions.

Obligatory Privacy Talk

Since May 2011, European Law requires a web site to request explicit consent from their visitors before they set most cookies, the exception being cookies needed for services requested explicitly by that visitor. Chances are your visitors did not ask to be tracked by Google Analytics (or any other kind of analytics that does tracking). You need to tell your visitors, explicitly ask for their consent, and react accordingly.


Resuming Instructions

To use Google Analytics, you need a Google Analytics Account, which can be tied to a regular Google Account. Let's assume you are the webmaster implementing this for someone else: ask for their Google Analytics Account identification. They may have to set one up, first.

This account identification is needed in the configuration of our integration code, named "analytics", which is part of the Required Parts Download, which we described in the Publizjr Installation Instructions. As the OmegaJunior Consultancy uses a copy of this same file, you can see its source at this web address. We have 3 copies of this file in the required downloads: 1 per language + 1 generic copy. All contain the same code.


Contents of the Analytics Translation

<script>/* <![CDATA[ */
var _gaq = _gaq || [ ];
_gaq.push(['_setAccount', 'UA-12345678-9']);
_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>


If you use it like this, the visitor clicks will not show up in the analysis. You need to do some configuring.


Configuring the Code

Substitute "UA-12345678-9" with the actual Google Analytics Account Identification.

Save the text and upload it to the hosting provider.


Adding it to the Template - Publizjr 1

Google advises to place their code at the bottom of our pages, above the body end tag </body>. So locate that in the Publizjr Generator Template of your choice, and above it, insert the following PHP code:
<?php
$strFilePath = "translations/analytics";
print( ReadPart( $strFilePath, false, '' ) );
?>


Or, if you're modifying a language-aware template, insert the following instead:
<?php
$strFilePath = "translations/$strRequestLang/analytics";
print( ReadPart( $strFilePath, false, '' ) );
?>


If your template and the translations folder have differing parents, adjust the $strFilePath accordingly.


Adding it to the Template - Publizjr 2

In Publizjr 2, you define in each template's configuration where it needs to look for its translations. And the language-aware templates automatically resolve the language sub folder. So all we have to do is save the script as a file named "analytics", place it in the correct translations folder, and add to our Template:

<?php 
$page->addTranslation( 'analytics' );
?>



Test

Save the template under a different name, upload it to the hosting provider, and test. When happy, copy the change to the live template.


If this fails to work for you, please let us know!

Interested?

Let's meet for coffee or over lunch. Talk to me using this form.