tilastokeskus/static/track.js
Mikko Ahlroth ba17fd8cef Progress
2018-06-26 00:18:53 +03:00

151 lines
5.4 KiB
JavaScript

/*
License for the docReady function below, from: https://github.com/jfriend00/docReady
The MIT License (MIT)
Copyright (c) 2014, John Friend
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
(function (funcName, baseObj) {
"use strict";
// The public function name defaults to window.docReady
// but you can modify the last line of this function to pass in a different object or method name
// if you want to put them in a different namespace and those will be used instead of
// window.docReady(...)
funcName = funcName || "docReady";
baseObj = baseObj || window;
var readyList = [];
var readyFired = false;
var readyEventHandlersInstalled = false;
// call this when the document is ready
// this function protects itself against being called more than once
function ready() {
if (!readyFired) {
// this must be set to true before we start calling callbacks
readyFired = true;
for (var i = 0; i < readyList.length; i++) {
// if a callback here happens to add new ready handlers,
// the docReady() function will see that it already fired
// and will schedule the callback to run right after
// this event loop finishes so all handlers will still execute
// in order and no new ones will be added to the readyList
// while we are processing the list
readyList[i].fn.call(window, readyList[i].ctx);
}
// allow any closures held by these functions to free
readyList = [];
}
}
function readyStateChange() {
if (document.readyState === "complete") {
ready();
}
}
// This is the one public interface
// docReady(fn, context);
// the context argument is optional - if present, it will be passed
// as an argument to the callback
baseObj[funcName] = function (callback, context) {
// if ready has already fired, then just schedule the callback
// to fire asynchronously, but right away
if (readyFired) {
setTimeout(function () { callback(context); }, 1);
return;
} else {
// add the function and context to the list
readyList.push({ fn: callback, ctx: context });
}
// if document already ready to go, schedule the ready function to run
// IE only safe when readyState is "complete", others safe when readyState is "interactive"
if (document.readyState === "complete" || (!document.attachEvent && document.readyState === "interactive")) {
setTimeout(ready, 1);
} else if (!readyEventHandlersInstalled) {
// otherwise if we don't have event handlers installed, install them
if (document.addEventListener) {
// first choice is DOMContentLoaded event
document.addEventListener("DOMContentLoaded", ready, false);
// backup is window load event
window.addEventListener("load", ready, false);
} else {
// must be IE
document.attachEvent("onreadystatechange", readyStateChange);
window.attachEvent("onload", ready);
}
readyEventHandlersInstalled = true;
}
}
})("docReady", window);
/*
* Tilastokeskus analytics tracking script.
* This hopefully works in IE 6+. To use, first set `window.tilastokeskus_url` to point to your
* Tilastokeskus instance. Then include this script and it will fire automatically.
*/
(function () {
if (!('XMLHttpRequest' in window)) {
window.XMLHttpRequest = function () {
return new ActiveXObject('Microsoft.XMLHTTP');
}
}
function send() {
var data = {};
if ('screen' in window && 'width' in window.screen) {
data['screen_width'] = window.screen.width;
data['screen_height'] = window.screen.height;
}
data.tz_offset = (new Date()).getTimezoneOffset();
data.url = window.location.href;
var data_parts = [];
function add_part(source, key, parts) {
if (key in source) parts.push(key + '=' + encodeURIComponent(data[key]));
}
add_part(data, 'screen_width', data_parts);
add_part(data, 'screen_height', data_parts);
add_part(data, 'tz_offset', data_parts);
add_part(data, 'url', data_parts);
var data_str = data_parts.join('&');
if ('tilastokeskus_url' in window) {
var req = new XMLHttpRequest();
req.open('POST', window.tilastokeskus_url);
req.setRequestHeader('content-type', 'application/x-www-form-urlencoded');
req.send(data_str);
}
else if ('console' in window) {
console.log('Tilastokeskus URL not set, would have sent data:', data_str);
}
}
// Send when DOM is hopefully available
docReady(send);
})();