

Goodbye Slovenia
David Lednik (red_mamba) migrating to New Zealand
Adam Savage drunk
Author: admin
I saw this on one blog, you have to see, hilarious
read comments (0)
Google Analytics for AJAX CakePHP pages
Author: admin
I posted this to CakePHP team an they rejected the modification because you have to change the core of the CakePHP frame work. So I’m posting this on my blog if someone finds it useful.
First thing is to change the default.ctp layout in the <head>;
java script does the actual ajax calls, in code we just change the url address, javascript calls the actual cake functions via ajax; it periodicaly scans for URL address changes, this way we have history in browser:
<script src=”http://www.google-analytics.com/urchin.js” type=”text/javascript”></script>
<script type=”text/javascript”>_uacct = “UA-XXXXXXX-X”;</script>
<script language=”javascript”>
var oldURL = ”;
var ignore = false;
function PhpAJAX(URL)
{
urchinTracker(URL);
oldURL = document.location.hash = URL;
}
//new PeriodicalExecuter(pollChatRoom, 1);
new PeriodicalExecuter(function(pe)
{
if (ignore == false)
{
if (oldURL != document.location.hash)
{
oldURL = document.location.hash;
new Ajax.Updater(’go’, oldURL.replace(/^.*#/, ”), {asynchronous:true, evalScripts:true, requestHeaders:['X-Update', 'go']});
}
}
}, 1);
</script>
Right before </body> tag (still in default.ctp) we add:
<script type=”text/javascript”>urchinTracker();</script>
Now we edit core.php file, at the end add the line:
define(’AJAX_ANALYTICS’, true);
Now lets edit ajax.php file, you’ll find it under
cake/libs/view/helpers:
function remoteFunction($options = null)
{
$func=”;
if (isset($options['update']))
{
/* TRACKING AJAX URLs FOR GOOGLE ANALYTICS */
if ( defined(’AJAX_ANALYTICS’) )
{
if (!isset($options['oldstyle']))
{
if (AJAX_ANALYTICS && $options['update']==’go’)
{
$func .= “PhpAJAX(’$options[url]‘);”;
return $func;
}
}
else
$func .= “urchinTracker(’$options[url]‘); “;
}
/**/
if (!is_array($options['update'])) {
$func .= “new Ajax.Updater(’{$options['update']}’,”;
} else {
$func .= “new Ajax.Updater(document.createElement(’div’),”;
}
if (!isset($options['requestHeaders'])) {
$options['requestHeaders'] = array();
}
if (is_array($options['update'])) {
$options['update'] = join(’ ‘, $options['update']);
}
$options['requestHeaders']['X-Update'] = $options['update'];
/**/
}
else
{
$func .= “new Ajax.Request(”;
}
$func .= “‘” . $this->url(isset($options['url']) ? $options['url'] : “”) . “‘”;
$func .= “, ” . $this->__optionsForAjax($options) . “)”;
if (isset($options['before'])) {
$func = “{$options['before']}; $func”;
}
if (isset($options['after'])) {
$func = “$func; {$options['after']};”;
}
if (isset($options['condition'])) {
$func = “if ({$options['condition']}) { $func; }”;
}
if (isset($options['confirm'])) {
$func = “if (confirm(’” . $this->Javascript->escapeString($options['confirm'])
. “‘)) { $func; } else { return false; }”;
}
//echo $func;
return $func;
}
That’s it
You can see this in action on my web hosting (gostovanje strani) pages.
