Skip to content

Commit

Permalink
js syntax fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
stuyam committed Feb 13, 2019
1 parent f9a3370 commit 8d3e0c2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/browser.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
var Browser = {
nameAndVersion(){
nameAndVersion() {
// http://stackoverflow.com/questions/5916900/how-can-you-detect-the-version-of-a-browser
var ua= navigator.userAgent, tem,
M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
if (/trident/i.test(M[1])) {
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
if (M[1]=== 'Chrome') {
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
Expand All @@ -16,11 +16,11 @@ var Browser = {
return M.join(' ');
},

isMobile(){
isMobile() {
return 'ontouchstart' in document;
},

userAgent(){
userAgent() {
return window.navigator.userAgent;
}
}
25 changes: 12 additions & 13 deletions src/cookie.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//http://www.w3schools.com/js/js_cookies.asp
var Cookie = {

prefix(){
prefix() {
return '__' + pixelFuncName + '_';
},

Expand All @@ -18,19 +18,19 @@ var Cookie = {
get(name) {
var name = this.prefix() + name + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
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;
},

delete(name){
delete(name) {
this.set(name,"",-100);
},

exists(name){
exists(name) {
return isset(this.get(name));
},

Expand All @@ -39,32 +39,31 @@ var Cookie = {
// this.set(name, 1, 10, window.location.pathname);
// },

setUtms(){
setUtms() {
var utmArray = ['utm_source','utm_medium','utm_term','utm_content','utm_campaign'];
var exists = false;
for(var i = 0, l = utmArray.length; i < l; i++){
if( isset(Url.getParameterByName(utmArray[i])) ){
for (var i = 0, l = utmArray.length; i < l; i++) {
if (isset(Url.getParameterByName(utmArray[i]))) {
exists = true;
break;
}
}
if(exists){
if (exists) {
var val, save = {};
for(var i = 0, l = utmArray.length; i < l; i++){
for (var i = 0, l = utmArray.length; i < l; i++) {
val = Url.getParameterByName(utmArray[i]);
if(isset(val)){
if (isset(val)) {
save[utmArray[i]] = val;
}
}
this.set('utm', JSON.stringify(save));
}
},

getUtm(name){
if(this.exists('utm')){
getUtm(name) {
if (this.exists('utm')) {
var utms = JSON.parse(this.get('utm'));
return name in utms ? utms[name] : "";
}
}

}
10 changes: 5 additions & 5 deletions src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// check if a variable is not undefined, null, or blank
var isset = function(variable){
var isset = function(variable) {
return typeof(variable) !== "undefined" && variable !== null && variable !== '';
}

var now = function(){
var now = function() {
return 1 * new Date;
}

Expand All @@ -16,12 +16,12 @@ var guid = function() {

// reduces all optional data down to a string
var optinalData = function(data) {
if(isset(data) === false) {
if (isset(data) === false) {
return '';
} else if(typeof data === 'object') {
} else if (typeof data === 'object') {
// runs optinalData again to reduce to string in case something else was returned
return optinalData(JSON.stringify(data));
} else if(typeof data === 'function') {
} else if (typeof data === 'function') {
// runs the function and calls optinalData again to reduce further if it isn't a string
return optinalData(data());
} else {
Expand Down
20 changes: 10 additions & 10 deletions src/pixel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Pixel {
constructor(event, timestamp, optinal){
constructor(event, timestamp, optinal) {
this.params = [];
this.event = event;
this.timestamp = timestamp;
Expand All @@ -8,16 +8,16 @@ class Pixel {
this.send();
}

buildParams(){
buildParams() {
var attr = this.getAttribute();
for(var index in attr) {
for (var index in attr) {
if (attr.hasOwnProperty(index)) {
this.setParam(index, attr[index](index));
}
}
}

getAttribute(){
getAttribute() {
return {
id: ()=>{return Config.id}, // website Id
uid: ()=>{return Cookie.get('uid')}, // user Id
Expand All @@ -43,23 +43,23 @@ class Pixel {
}
}

setParam(key, val){
if(isset(val)){
setParam(key, val) {
if (isset(val)) {
this.params.push(key+'='+val);
} else {
this.params.push(key+'=');
}
}

send(){
send() {
window.navigator.sendBeacon ? this.sendBeacon() : this.sendImage();
}

sendBeacon(){
sendBeacon() {
window.navigator.sendBeacon(this.getSourceUrl());
}

sendImage(){
sendImage() {
this.img = document.createElement('img');
this.img.src = this.getSourceUrl();
this.img.style.display = 'none';
Expand All @@ -68,7 +68,7 @@ class Pixel {
document.getElementsByTagName('body')[0].appendChild(this.img);
}

getSourceUrl(){
getSourceUrl() {
return pixelEndpoint + '?' + encodeURI(this.params.join('&'));
}
}
10 changes: 5 additions & 5 deletions src/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Cookie.setUtms();

// process the queue and future incoming commands
pixelFunc.process = function(method, value, optinal) {
if(method == 'init') {
if (method == 'init') {
Config.id = value;
} else if(method == 'event') {
if(value == 'pageload' && !Config.pageLoadOnce){
Expand All @@ -20,18 +20,18 @@ pixelFunc.process = function(method, value, optinal) {
}

// run the queued calls from the snippet to be processed
for(var i = 0, l = pixelFunc.queue.length; i < l; i++) {
for (var i = 0, l = pixelFunc.queue.length; i < l; i++) {
pixelFunc.process.apply(pixelFunc, pixelFunc.queue[i]);
}

window.addEventListener('unload', function() {
if(!Config.pageCloseOnce){
if (!Config.pageCloseOnce) {
Config.pageCloseOnce = true;
// set 10 minutes page close cookie
// Cookie.throttle('pageclose');
new Pixel('pageclose', now(), function(){
// if a link was clicked in the last 5 seconds that goes to an external host, pass it through as event data
if(isset(Config.externalHost) && (now() - Config.externalHost.time) < 5*1000){
if (isset(Config.externalHost) && (now() - Config.externalHost.time) < 5*1000) {
return Config.externalHost.link;
}
});
Expand All @@ -42,7 +42,7 @@ window.onload = function() {
var aTags = document.getElementsByTagName('a');
for (var i = 0, l = aTags.length; i < l; i++) {
aTags[i].addEventListener('click', function(e) {
if(Url.externalHost(this)){
if (Url.externalHost(this)) {
Config.externalHost = {link:this.href, time:now()};
}
}.bind(aTags[i]));
Expand Down

0 comments on commit 8d3e0c2

Please sign in to comment.