Web-Transfer-Javascript

Transfer data from web-pages

How to use the library

Include the library and send data:

1
2
3
4
<script src="js/transfer.js"></script>`
var lightclientid=999; // your individual id on your server
var jData={'label':'/MyShop/Start/SomeCategory'};
lightspeed.track(jData);

Which values to use : ‘label,name,content,site,pageid,….’ ?
You are free to choose whatever you want, except of ‘clientid’, ‘user’ and ‘trackingtime’. They are used internaly.The data you send to the server are returned to the other client and can be processed there.

The library

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
var lightspeed = (function (win, doc) {
//****************************************************************************
// start internal functions
if(getCookie("lightuser")==""){
setCookie("lightuser", generateUUID(),1000 )
}
var lightuser = window.lightuser ? window.lightuser : getCookie("lightuser");
var clientid = window.lightclientid ? window.lightclientid : "0" ;
var contentObject ={};
//****************************************************************************
function addimage() {
lightuser = window.lightuser ? window.lightuser : getCookie("lightuser");
var img = document.createElement("img");
img.id="pic4711";
//img.src = getPicServer() +"/" + clientid + "/" + lightuser + "/" + getImageName() +".jpg" + getImageParam();
//img.src = getPicServer() +"/" + clientid + "/" + lightuser + "/0.png" + getImageParam();
img.src = getPicServer() + "/0.png" + getImageParam();
img.height = 0;
img.width = 0;
img.style.top=0;
img.style.right=0;
img.onload = loadHandler;
document.body.appendChild(img);
}
//****************************************************************************
function getPicServer(){
return "http://light-no.de/img";
}
//****************************************************************************
function getImageParam(){
if(!isEmpty(contentObject )){
contentObject.clientid = window.lightclientid ? window.lightclientid : "0" ;;
contentObject.user = lightuser;
contentObject.trackingtime = new Date().getTime();
var va = JSON.stringify(contentObject);
//console.log(contentObject);
va = b64EncodeUnicode(va);
contentObject={};
return "?l=" + va + "&t=" + rand(1,50000);
} else{
return "";
}
}
//****************************************************************************
function rand (min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//****************************************************************************
//****************************************************************************
function isEmpty(obj) {
for (var x in obj) { return false; }
return true;
}
//***************************************************************************
function loadHandler(){
//console.log("image loaded");
}
//****************************************************************************
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}
//***************************************************************************
function b64EncodeUnicode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
//b64EncodeUnicode('? à la mode'); // "4pyTIMOgIGxhIG1vZGU="
//b64EncodeUnicode('\n'); // "Cg=="
//***************************************************************************
function b64DecodeUnicode(str) {
return decodeURIComponent(Array.prototype.map.call(atob(str), function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
//***************************************************************************
function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
//***************************************************************************
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+ d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
//***************************************************************************
//http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
function generateUUID(){
var d = new Date().getTime();
if(window.performance && typeof window.performance.now === "function"){
d += performance.now(); //use high-precision timer if available
}
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = (d + Math.random()*16)%16 | 0;
d = Math.floor(d/16);
return (c=='x' ? r : (r&0x3|0x8)).toString(16);
});
return uuid;
}
// end internal function
//***************************************************************************
//***************************************************************************
// return the public methods
return {
generateUUID :function(){ return generateUUID();},
track : function ( param) {
contentObject = param;
addimage();
}
};
//***************************************************************************
//***************************************************************************
})(window, document);

Nothing really hard to get. Anyway, let´s have a look at some parts.
The track-function takes a Json-object and calls addimage(); which transforms the value to Base64 and append it as an image to the document.

Before this happens, as visitor-cookie is set to random value. Maybe helpfull to get well-defined users. As a lot of apps can run on one server, it is neccesary to set a clientid. If you request data from the server, you have got to use the same clientid.
The created-image-link will be something like:
http://light-no.de/img/0.png?l=eyJjYXRlZ29yeSI6IlN0YXJ0c2VpdGUvQXV0byAmIE1vdG9ycmFkL0tGWi1FcnNhdHp0ZWlsZS9TY2hlaWJlbnJlaW5pZ3VuZy9TY2hlaWJlbndpc2NoZXIiLCJjb250ZW50Ijoiw5xiZXJibGljayIsImNsaWVudGlkIjoxMDAwLCJ1c2VyIjoiNTEyOTZlYTMtYzk3My00ZTBiLWFkMjItZGEwMDU2YmVlMzU5IiwidHJhY2tpbmd0aW1lIjoxNDkwMDI5MjE5Mzc0fQ==&t=18162

The l-Param contains the data, the t-param is a random number, to avoid caching.