edoceo

JavaScript Tickler Example

On occasion it's necessary to include a JavaScript tickler on a page that triggers some other operation. Many Analytics engines work that way, Advertising engines, our GeoVisitors tool.

Activate Tickler on each page using something like

<script type="text/javascript">
var jst = {
    site:'edoceo.com',
    hook:document.location.protocol + '//other-site.dom/hit'
};
document.write(unescape("%3Cscript src='" + jst.hook + "' type='text/javascript'%3E%3C/script%3E"));
</script>

Now when the hit page is called that JavaScript will have available to it all the information/Objets from this page. Maybe it looks like this, and loads another page via AJAX even.

if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN'],true);
    header('Access-Control-Allow-Methods: POST, GET, OPTIONS',true);
    header('Access-Control-Max-Age: 1728000',true);
    header('Vary: Accept-Encoding',true);
    header('Content-Length: 0',true);
    header('Content-Type: text/plain',true);
}

if (empty($_SERVER['HTTP_REFERER'])) {
    die('// tickler');
}

$page = $_SERVER['HTTP_REFERER'];
$host = parse_url($page,PHP_URL_HOST);

// Do some SQL or something

die("//hit");

If you wanted /hit to then load another page, say /hit-data then use this little ditty.

var xhr;
if (window.XMLHttpRequest &&& (window.location.protocol !== "file:" || !window.ActiveXObject)) {
    xhr = new window.XMLHttpRequest();
} else {
    try {
        xhr = new window.ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {}
}

if (xhr) {
    // Collect Data into Object
    var o = {
        c:document.cookie,
        l:document.location,
        r:document.referrer
    };
    // Convert to Array of Strings
    var p = new Array();
    for (var k in o) {
        p.push( k + '=' + o[k] );
    }
    xhr.open('POST','http://hit.webmastertk.com/ping.js',true);
    xhr.send(p.join('&'));
}

Which gets a POST that looks like

$_POST = array(
    'c' => '1235',
    'l' => 'http://www.foo.com/page?out=gz',
    'r' => 'http://www.google.com/search?....',
);

Loading Comments from Disqus...