keen-tracking
Advanced tools
Comparing version 0.0.1 to 0.0.3
@@ -30,2 +30,5 @@ ;(function (f) { | ||
var root = 'undefined' !== typeof window ? window : this; | ||
var previousKeen = root.Keen; | ||
// ------------------------ | ||
@@ -32,0 +35,0 @@ // Methods |
@@ -8,5 +8,2 @@ var Emitter = require('component-emitter'); | ||
var root = this; | ||
var previousKeen = root.Keen; | ||
var Keen = function(config){ | ||
@@ -88,3 +85,3 @@ this.configure(config); | ||
} | ||
url = this.config.protocol + '://' + this.config.host + '/3.0/projects/' + this.projectId(); | ||
url = this.config.protocol + '://' + this.config.host; | ||
if (path) { | ||
@@ -91,0 +88,0 @@ url += path; |
@@ -21,9 +21,12 @@ var Keen = require('./index'); | ||
function recordEvent(eventCollection, eventBody, callback){ | ||
var url, data, cb, getRequestUrl, extendedEventBody; | ||
function recordEvent(eventCollection, eventBody, callback, async){ | ||
var url, data, cb, getRequestUrl, getRequestUrlOkLength, extendedEventBody, isAsync; | ||
url = this.url('/events/' + encodeURIComponent(eventCollection)); | ||
url = this.url(this.writePath() + '/' + encodeURIComponent(eventCollection)); | ||
data = {}; | ||
cb = callback; | ||
// Requests are asynchronous by default | ||
isAsync = ('boolean' === typeof async) ? async : true; | ||
if (!checkValidation.call(this, cb)) { | ||
@@ -66,3 +69,3 @@ return; | ||
getRequestUrl = this.url('/events/' + encodeURIComponent(eventCollection), { | ||
getRequestUrl = this.url(this.writePath() + '/' + encodeURIComponent(eventCollection), { | ||
api_key : this.writeKey(), | ||
@@ -72,21 +75,34 @@ data : base64.encode(JSON2.stringify(extendedEventBody)), | ||
}); | ||
getRequestUrlOkLength = getRequestUrl.length < getUrlMaxLength(); | ||
if (getRequestUrl.length < getUrlMaxLength()) { | ||
if (isAsync) { | ||
switch (this.config.requestType) { | ||
case 'xhr': | ||
sendXhr.call(this, 'GET', getRequestUrl, null, null, cb); | ||
sendXhr.call(this, 'POST', url, extendedEventBody, cb); | ||
break; | ||
case 'beacon': | ||
sendBeacon.call(this, getRequestUrl, cb); | ||
if (getRequestUrlOkLength) { | ||
sendBeacon.call(this, getRequestUrl, cb); | ||
} | ||
else { | ||
attemptPostXhr.call(this, url, extendedEventBody, | ||
'Beacon URL length exceeds current browser limit, and XHR is not supported.', cb) | ||
} | ||
break; | ||
default: | ||
sendJSONp.call(this, getRequestUrl, cb); | ||
if (getRequestUrlOkLength) { | ||
sendJSONp.call(this, getRequestUrl, cb); | ||
} | ||
else { | ||
attemptPostXhr.call(this, url, extendedEventBody, | ||
'JSONp URL length exceeds current browser limit, and XHR is not supported.', cb) | ||
} | ||
break; | ||
} | ||
} | ||
else if (getXhr()) { | ||
sendXhr.call(this, 'POST', url, extendedEventBody, cb); | ||
} | ||
else { | ||
handleValidationError.call(this, 'URL length exceeds current browser limit, and XHR is not supported.'); | ||
// Send synchronous request | ||
if (getRequestUrlOkLength) { | ||
sendSynchronousXhr(getRequestUrl); | ||
} | ||
} | ||
@@ -98,4 +114,2 @@ | ||
// ------------------------------ | ||
@@ -108,3 +122,3 @@ // .recordEvents | ||
url = this.url('/events'); | ||
url = this.url(this.writePath()); | ||
cb = callback; | ||
@@ -244,2 +258,11 @@ callback = null; | ||
function attemptPostXhr(url, data, noXhrError, callback) { | ||
if (getXhr()) { | ||
sendXhr.call(this, 'POST', url, data, callback); | ||
} | ||
else { | ||
handleValidationError.call(this, noXhrError); | ||
} | ||
} | ||
function sendXhr(method, url, data, callback){ | ||
@@ -294,2 +317,10 @@ var self = this; | ||
function sendSynchronousXhr(url){ | ||
var xhr = getXhr(); | ||
if (xhr) { | ||
xhr.open('GET', url, false); | ||
xhr.send(null); | ||
} | ||
} | ||
function getXhr() { | ||
@@ -296,0 +327,0 @@ // yay, superagent! |
@@ -25,3 +25,2 @@ var Keen = require('./index'); | ||
url = this.url('/events/' + encodeURIComponent(eventCollection)); | ||
data = {}; | ||
@@ -78,3 +77,2 @@ cb = callback; | ||
url = this.url('/events'); | ||
cb = callback; | ||
@@ -81,0 +79,0 @@ callback = null; |
@@ -22,8 +22,17 @@ var Cookies = require('cookies-js'); | ||
cookie.prototype.get = function(str){ | ||
var data = Cookies.get(this.config.key) ? JSON2.parse( decodeURIComponent(Cookies.get(this.config.key)) ) : {}; | ||
return (str && typeof data[str] !== 'undefined') ? data[str] : data; | ||
var data = {}; | ||
if (Cookies.get(this.config.key)) { | ||
data = JSON2.parse( decodeURIComponent(Cookies.get(this.config.key)) ); | ||
} | ||
if (str) { | ||
return ('undefined' !== typeof data[str]) ? data[str] : null; | ||
} | ||
else { | ||
return data; | ||
} | ||
}; | ||
cookie.prototype.set = function(str, value){ | ||
if (!arguments.length) return this; | ||
if (!arguments.length || !this.enabled()) return this; | ||
if ('string' === typeof str && arguments.length === 2) { | ||
@@ -50,1 +59,5 @@ this.data[str] = value ? value : null; | ||
}; | ||
cookie.prototype.enabled = function(){ | ||
return Cookies.enabled; | ||
}; |
{ | ||
"name": "keen-tracking", | ||
"version": "0.0.1", | ||
"version": "0.0.3", | ||
"main": "index.js", | ||
@@ -56,3 +56,2 @@ "browser": "lib/browser.js", | ||
"karma-sauce-launcher": "^0.2.11", | ||
"karma-sinon": "^1.0.4", | ||
"mocha": "^2.2.5", | ||
@@ -63,3 +62,2 @@ "moment": "^2.10.3", | ||
"proclaim": "^3.3.0", | ||
"sinon": "^1.14.1", | ||
"vinyl-buffer": "^1.0.0", | ||
@@ -66,0 +64,0 @@ "vinyl-source-stream": "^1.1.0" |
@@ -39,3 +39,3 @@ # keen-tracking.js [![Build Status](https://travis-ci.org/keen/keen-tracking.js.svg?branch=master)](https://travis-ci.org/keen/keen-tracking.js) | ||
There are several new methods and name changes from keen-js, but fear not! We have included shims and legacy methods to make this library fully backward-compatible with the core functionality of keen-js. Here are the methods and their replacement methods: | ||
There are several new methods and name changes from keen-js, but fear not! We have included shims and legacy methods to make this library fully backward-compatible with the core functionality of keen-js, aside from one breaking change to the `client.url()` method (detailed below). Here are the methods and their replacement methods: | ||
* `addEvent` and `addEvents` are now [`recordEvent`](#record-a-single-event) and [`recordEvents`](#record-multiple-events) | ||
@@ -47,2 +47,11 @@ * `setGlobalProperties` is now handled by the [`extendEvents`](#extend-events) methods | ||
**Breaking change from keen-js:** the previous implementation of `client.url()` automatically included `https://api.keen.io/3.0/projects/PROJECT_ID` + a `path` argument ('/events/whatever'), which severely limited its value. It now only returns `https://api.keen.io` + the path argument. | ||
You can also now pass in an object to append a serialized query string to the result, like so: | ||
```javascript | ||
var url = client.url('/3.0/projects', { key: 'value'} ); | ||
// https://api.keen.io/3.0/projects?key=value | ||
``` | ||
<a name="additional-resources"></a> | ||
@@ -89,3 +98,3 @@ **Additional resources:** | ||
var latest,prev=name!=='Keen'&&window.Keen?window.Keen:false;ctx[name]=ctx[name]||{ready:function(fn){var h=document.getElementsByTagName('head')[0],s=document.createElement('script'),w=window,loaded;s.onload=s.onerror=s.onreadystatechange=function(){if((s.readyState&&!(/^c|loade/.test(s.readyState)))||loaded){return}s.onload=s.onreadystatechange=null;loaded=1;latest=w.Keen;if(prev){w.Keen=prev}else{try{delete w.Keen}catch(e){w.Keen=void 0}}ctx[name]=latest;ctx[name].ready(fn)};s.async=1;s.src=path;h.parentNode.insertBefore(s,h)}} | ||
}('Keen','https://d26b395fwzu5fz.cloudfront.net/keen-tracking-0.0.1.min.js',this); | ||
}('Keen','https://d26b395fwzu5fz.cloudfront.net/keen-tracking-0.0.3.min.js',this); | ||
@@ -428,9 +437,9 @@ // Executes when the library is loaded and ready | ||
```javascript | ||
var session = Keen.utils.cookie('visitor-stats'); | ||
var sessionCookie = Keen.utils.cookie('visitor-stats'); | ||
// Set a single value | ||
session.set('user_id', '222323843234'); | ||
sessionCookie.set('user_id', '222323843234'); | ||
// Set multiple values | ||
session.set({ | ||
sessionCookie.set({ | ||
user_id: '222323843234', | ||
@@ -440,13 +449,18 @@ first_referrer: 'https://github.com/keen/keen-tracking.js' | ||
// Get a single value | ||
session.get('user_id'); // '222323843234' | ||
// Get a single value, if it exists | ||
sessionCookie.get('user_id'); | ||
// Returns '222323843234' or null | ||
// Get all values | ||
session.get(); // { user_id: '222323843234' } | ||
sessionCookie.get(); | ||
// Returns { user_id: '222323843234' } | ||
sessionCookie.enabled(); | ||
// Returns true or false | ||
// Expire the cookie | ||
session.expire(); | ||
sessionCookie.expire(); | ||
// Set options on the cookie | ||
session.options({ | ||
sessionCookie.options({ | ||
domain: '...', | ||
@@ -457,2 +471,4 @@ secure: true | ||
**Important:** Some browsers do not allow cookies to be created or accessed from a local file (`file://dev/index.html`), which can make local development and testing problematic. `.set()` and `.get()` methods will only function correctly when cookies are enabled. | ||
This utility uses [ScottHamper's](https://github.com/ScottHamper) wonderfully simple [Cookies.js](https://github.com/ScottHamper/Cookies) library. Read all options for Cookies.js [here](https://github.com/ScottHamper/Cookies#cookiessetkey-value--options). | ||
@@ -771,3 +787,3 @@ | ||
title: document.title, | ||
url: document.href | ||
url: document.location.href | ||
// info: {} (add-on) | ||
@@ -774,0 +790,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
80340
32
30
1589
894
1