Where do I put the code for Open Web Analytics

I’d like to add the tracking code for Open Web Analytics to my Backlight 6 Albums. Where do I put the code? I believe it’s supposed to be in the footer of the source code for each HTML page.

Charles

The scripts hook of a phplugins file might be the best spot. That will insert code before the closing body tag.

I think you can add code that will first check if the page is album and if it is, it will add the code to the page. In the phplugins-angolin-sample.php file there’s a sample function starting on line 509 that uses an if statement to check for an album before loading more code.

So yours might look like:

function scripts()
{
	if ($this->hasAlbum()) {
		---your code---
	}
	return false;
} // END /**/	

On line 400 of the sample phplugins file is a sample of adding code to the script hook

If using Kookaburra, be sure to use the Kookaburra sample file as your starting point.

@Matthew or @Daniel may have better ideas

It’s Javascript so I put it inside the PHP and tried escaping the single quotes with backslash:

function scripts()
{
	if ($this->hasAlbum()) {
        //<![CDATA[
        var owa_baseUrl = \'https://MYWEBSITE.COM/owa/\';
        var owa_cmds = owa_cmds || [];
        owa_cmds.push([\'setSiteId\', \'TRACKING_CODE\']);
        owa_cmds.push([\'trackPageView\']);
        owa_cmds.push([\'trackClicks\']);

        (function() {
            var _owa = document.createElement(\'script\'); _owa.type = \'text/javascript\'; _owa.async = true;
            owa_baseUrl = (\'https:\' == document.location.protocol ? window.owa_baseSecUrl || owa_baseUrl.replace(/http:/, \'https:\') : owa_baseUrl );
            _owa.src = owa_baseUrl + \'modules/base/dist/owa.tracker.js\';
            var _owa_s = document.getElementsByTagName(\'script\')[0]; _owa_s.parentNode.insertBefore(_owa, _owa_s);
        }());
    //]]>

    }
	return false;
} // END /**/	

Looks like the http// doesn’t work. Do you know how to escape that part? It’s not showing up in the source code of the page.

You might try using the other format for adding scripts. It’s shown in the sample file.

Is just the http part, or the whole code not appearing?
can you post a link to an album?

I’m wondering if this is the problem as the part after // is greyed out above.
I changed the name of the site here but not in my code.

Your code treats the javascript code as php code. You need to insert it using echo so it’s properly inserted in your page. Here’s the basic example without your site specifics:

function scripts()
{
    if ($this->hasAlbum()) {
           echo " 
//<![CDATA[
var owa_baseUrl = 'http://your.domain.com/path/to/owa/';
var owa_cmds = owa_cmds || [];
owa_cmds.push(['setSiteId', 'your_site_id']);
owa_cmds.push(['trackPageView']);
owa_cmds.push(['trackClicks']);
owa_cmds.push(['trackDomStream']);

(function() {
	var _owa = document.createElement('script'); _owa.type = 'text/javascript'; _owa.async = true;
	_owa.src = owa_baseUrl + 'modules/base/js/owa.tracker-combined-min.js';
	var _owa_s = document.getElementsByTagName('script')[0]; _owa_s.parentNode.insertBefore(_owa, _owa_s);
}());
//]]>
 ";
    }
	return false;
} // END /**/	

Thanks Daniel and Rod. I finally got it working using the code below. As the sample indicates “scripts” loads the content directly above the </body> tag which is where you want it to be.

Charles

<?php

function user_load($style, $path) {
	$g_tsvrl = explode(' ', $style); // Extract gallery type
	define ('G_STYLE', strtoupper($g_tsvrl[1])); // and set global for later
	$g_path = str_ireplace('\\','/',$path); // change \ to /
	$chunks = explode('/',$g_path); // and put into array
	define ('G_PATH', strtoupper($chunks[count($chunks)-2])); // gallery folder name is second to last
	//define ( 'TTG_SITE', ''); // set new site root for navigation, resources, etc.
}

if (defined('BACKLIGHT_HOOK')) {
	require_once(realpath(BACKLIGHT_HOOK).'/modules/module-designer/application/helpers/APHPlugins.php');
}

class PHPlugins extends APHPlugins
{
	function scripts()
	{
		if ($this->hasAlbum()) {
			echo"
            <script type=\"text/javascript\">            
            //<![CDATA[
            var owa_baseUrl = 'https://MYWEBSITE.COM/owa/';
            var owa_cmds = owa_cmds || [];
            owa_cmds.push(['setSiteId', 'TRACKING_CODE']);
            owa_cmds.push(['trackPageView']);
            owa_cmds.push(['trackClicks']);

            (function() {
                var _owa = document.createElement('script'); _owa.type = 'text/javascript'; _owa.async = true;
                owa_baseUrl = ('https:' == document.location.protocol ? window.owa_baseSecUrl || owa_baseUrl.replace(/http:/, 'https:') : owa_baseUrl );
                _owa.src = owa_baseUrl + 'modules/base/dist/owa.tracker.js';
                var _owa_s = document.getElementsByTagName('script')[0]; _owa_s.parentNode.insertBefore(_owa, _owa_s);
            }());
            //]]>
            </script>            
            ";       
        }
	
		return true;
	} // END /**/

} ?>