TIP: Using SlimStat from JavaScript

24 views
Skip to first unread message

Tekl

unread,
Jan 22, 2010, 7:37:15 PM1/22/10
to SlimStat
Hi,

if you want to track clicks in JavaScript when only a part of the page
is changed (AJAX) the you should modify _lib/js.php as following:

<?php

/*
* SlimStat: simple web analytics
* Copyright (C) 2010 Pieces & Bits Limited
*
* This program is free software; you can redistribute it and/or
modify
* it under the terms of the GNU General Public License as published
by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
along
* with this program; if not, write to the Free Software Foundation,
Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

$base_href = dirname( $_SERVER['SCRIPT_NAME'] );

header( 'Content-type: text/javascript; charset=UTF-8' );

?>
function slimstatAddLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}

slimstatAddLoadEvent(slimstatTrackingImage(''));

function slimstatTrackingImage(url) {
if (url == '') { url = document.URL; }
var ssSrc = '<?php echo $base_href; ?>/stats_js.php?ref=' +
encodeURIComponent(document.referrer)
+ '&url=' + encodeURIComponent(url)
+ '&res=' + encodeURIComponent(screen.width+'x'+screen.height)
+ '&ttl=' + encodeURIComponent(document.title)
+ '&ts=<?php echo time(); ?>';

var ssImg = document.createElement('img');
ssImg.setAttribute('id', 'slimstat<?php echo htmlspecialchars
( SlimStat::app_version() ); ?>img');
ssImg.setAttribute('src', ssSrc);
ssImg.setAttribute('style', 'position:absolute;top:-10px;left:0');
ssImg.setAttribute('width', '1');
ssImg.setAttribute('height', '1');
ssImg.setAttribute('alt', '');
oldImg = document.getElementById('slimstat<?php echo htmlspecialchars
( SlimStat::app_version() ); ?>img');
if (oldImg) { document.body.removeChild(oldImg); }
document.body.appendChild(ssImg);
}


Now you can call slimstatTrackingImage(document.URL.replace(/#/,'?'))
to get your bookmarkable page modifications tracked.

Tekl

unread,
Feb 8, 2010, 6:36:40 PM2/8/10
to SlimStat
Internet Explorer does not love my changes. Here is a new file. As I’m
no JS crack I don’t know how to do it without the code duplication.

<?php

function slimstatTrackingImage(url) {


if (url == '') { url = document.URL; }
var ssSrc = '<?php echo $base_href; ?>/stats_js.php?ref=' +
encodeURIComponent(document.referrer)
+ '&url=' + encodeURIComponent(url)
+ '&res=' + encodeURIComponent(screen.width+'x'+screen.height)
+ '&ttl=' + encodeURIComponent(document.title)
+ '&ts=<?php echo time(); ?>';

var ssImg = document.createElement('img');
ssImg.setAttribute('id', 'slimstat<?php echo

htmlspecialchars( SlimStat::app_version() ); ?>img');


ssImg.setAttribute('src', ssSrc);
ssImg.setAttribute('style', 'position:absolute;top:-10px;left:0');
ssImg.setAttribute('width', '1');
ssImg.setAttribute('height', '1');
ssImg.setAttribute('alt', '');
oldImg = document.getElementById('slimstat<?php echo

htmlspecialchars( SlimStat::app_version() ); ?>img');
if (oldImg) { document.body.removeChild(oldImg); }
document.body.appendChild(ssImg);
}

if (document.URL.indexOf('#') == -1) {
slimstatAddLoadEvent(function() {


var ssSrc = '<?php echo $base_href; ?>/stats_js.php?ref=' +
encodeURIComponent(document.referrer)

+ '&url=' + encodeURIComponent(document.URL)


+ '&res=' + encodeURIComponent(screen.width+'x'+screen.height)
+ '&ttl=' + encodeURIComponent(document.title)
+ '&ts=<?php echo time(); ?>';

var ssImg = document.createElement('img');
ssImg.setAttribute('id', 'slimstat<?php echo

htmlspecialchars( SlimStat::app_version() ); ?>img');


ssImg.setAttribute('src', ssSrc);
ssImg.setAttribute('style', 'position:absolute;top:-10px;left:0');
ssImg.setAttribute('width', '1');
ssImg.setAttribute('height', '1');
ssImg.setAttribute('alt', '');

document.body.appendChild(ssImg);
});
}

gopichand duggirala

unread,
Feb 9, 2010, 1:08:57 AM2/9/10
to slim...@googlegroups.com, rumbo...@gmail.com
Hi

   Can you please let me know,where i should use this javascript code.

Regards,
Gopichand.D


--
You received this message because you are subscribed to the Google Groups "SlimStat" group.
To post to this group, send email to slim...@googlegroups.com.
To unsubscribe from this group, send email to slimstat+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/slimstat?hl=en.




--
Gopichnad.D
Reply all
Reply to author
Forward
0 new messages