keen-tracking
Advanced tools
Comparing version 0.0.5 to 0.1.0
191
lib/index.js
@@ -8,49 +8,80 @@ var Emitter = require('component-emitter'); | ||
var Keen = function(config){ | ||
var K = function(config){ | ||
var self = this; | ||
this.configure(config); | ||
Keen.emit('client', this); | ||
}; | ||
Keen.prototype.configure = function(config){ | ||
var self = this, defaultProtocol; | ||
extend(this.config.resources, K.resources); | ||
if (config['host']) { | ||
config['host'].replace(/.*?:\/\//g, ''); | ||
this.extensions = { | ||
events: [], | ||
collections: {} | ||
}; | ||
this.queue = queue(); | ||
this.queue.on('flush', function(){ | ||
self.recordDeferredEvents(); | ||
}); | ||
if (K.debug) { | ||
this.on('error', K.log); | ||
} | ||
defaultProtocol = 'https'; | ||
// IE<10 request shim | ||
if ('undefined' !== typeof document && document.all) { | ||
config['protocol'] = (document.location.protocol !== 'https:') ? 'http' : defaultProtocol; | ||
this.emit('ready'); | ||
K.emit('client', this); | ||
}; | ||
Emitter(K); | ||
Emitter(K.prototype); | ||
extend(K, { | ||
debug: false, | ||
enabled: true, | ||
loaded: false, | ||
helpers: {}, | ||
resources: { | ||
'base' : '{protocol}://{host}', | ||
'version' : '{protocol}://{host}/3.0', | ||
'projects' : '{protocol}://{host}/3.0/projects', | ||
'projectId' : '{protocol}://{host}/3.0/projects/{projectId}', | ||
'events' : '{protocol}://{host}/3.0/projects/{projectId}/events' | ||
}, | ||
utils: {}, | ||
version: '__VERSION__' | ||
}); | ||
K.log = function(message) { | ||
if (K.debug && typeof console == 'object') { | ||
console.log('[Keen IO]', message); | ||
} | ||
}; | ||
self.config = self.config || { | ||
// projectId | ||
// writeKey | ||
K.prototype.configure = function(cfg){ | ||
var self = this, | ||
config = cfg || {}, | ||
defaultProtocol = 'https'; | ||
this.config = this.config || { | ||
projectId: undefined, | ||
writeKey: undefined, | ||
host: 'api.keen.io', | ||
protocol: defaultProtocol, | ||
requestType: 'jsonp' | ||
// writePath (generated) | ||
requestType: 'jsonp', | ||
resources: {}, | ||
writePath: undefined | ||
}; | ||
extend(self.config, config || {}); | ||
// IE<10 request shim | ||
if ('undefined' !== typeof document && document.all) { | ||
config['protocol'] = (document.location.protocol !== 'https:') ? 'http' : defaultProtocol; | ||
} | ||
if (config['host']) { | ||
config['host'].replace(/.*?:\/\//g, ''); | ||
} | ||
self.queue = queue(); | ||
self.queue.on('flush', function(){ | ||
self.recordDeferredEvents(); | ||
}); | ||
self.extensions = { | ||
events: [], | ||
collections: {} | ||
}; | ||
if (Keen.debug) { | ||
self.on('error', Keen.log); | ||
} | ||
self.emit('ready'); | ||
extend(this.config, config); | ||
return self; | ||
}; | ||
Keen.prototype.projectId = function(str){ | ||
K.prototype.projectId = function(str){ | ||
if (!arguments.length) return this.config.projectId; | ||
@@ -61,3 +92,3 @@ this.config.projectId = (str ? String(str) : null); | ||
Keen.prototype.writeKey = function(str){ | ||
K.prototype.writeKey = function(str){ | ||
if (!arguments.length) return this.config.writeKey; | ||
@@ -68,35 +99,59 @@ this.config.writeKey = (str ? String(str) : null); | ||
Keen.prototype.writePath = function(str){ | ||
if (!arguments.length) { | ||
if (!this.projectId()) { | ||
this.emit('error', 'Keen is missing a projectId property'); | ||
return; | ||
} | ||
return this.config.writePath ? this.config.writePath : ('/3.0/projects/' + this.projectId() + '/events'); | ||
K.prototype.resources = function(obj){ | ||
if (!arguments.length) return this.config.resources; | ||
var self = this; | ||
if (typeof obj === 'object') { | ||
each(obj, function(value, key){ | ||
self.config.resources[key] = (value ? value : null); | ||
}); | ||
} | ||
this.config.writePath = (str ? String(str) : null); | ||
return this; | ||
}; | ||
Keen.prototype.url = function(path, data){ | ||
var url; | ||
if (!this.projectId()) { | ||
this.emit('error', 'Keen is missing a projectId property'); | ||
return; | ||
K.prototype.url = function(name){ | ||
var args = Array.prototype.slice.call(arguments, 1), | ||
baseUrl = K.resources.base || '{protocol}://{host}', | ||
path; | ||
if (name && typeof name === 'string') { | ||
if (this.config.resources[name]) { | ||
path = this.config.resources[name]; | ||
} | ||
else { | ||
path = baseUrl + name; | ||
} | ||
} | ||
url = this.config.protocol + '://' + this.config.host; | ||
if (path) { | ||
url += path; | ||
else { | ||
path = baseUrl; | ||
} | ||
if (data) { | ||
url += '?' + serialize(data); | ||
} | ||
return url; | ||
each(this.config, function(value, key){ | ||
if (typeof value !== 'object') { | ||
path = path.replace('{' + key + '}', value); | ||
} | ||
}); | ||
each(args, function(arg, i){ | ||
if (typeof arg === 'string') { | ||
path += '/' + arg; | ||
} | ||
else if (typeof arg === 'object') { | ||
path += '?'; | ||
each(arg, function(value, key){ | ||
path += key + '=' + value + '&'; | ||
}); | ||
path = path.slice(0, -1); | ||
} | ||
}); | ||
return path; | ||
}; | ||
// ---------------------- | ||
// DEPRECATED | ||
// ---------------------- | ||
Keen.prototype.setGlobalProperties = function(props){ | ||
this.emit('error', 'This method has been deprecated. Check out #extendEvents: https://github.com/keen/keen-tracking.js#extend-events'); | ||
K.prototype.setGlobalProperties = function(props){ | ||
K.log('This method has been deprecated. Check out #extendEvents: https://github.com/keen/keen-tracking.js#extend-events'); | ||
if (!props || typeof props !== 'function') { | ||
@@ -110,20 +165,14 @@ this.emit('error', 'Invalid value for global properties: ' + props); | ||
Emitter(Keen); | ||
Emitter(Keen.prototype); | ||
extend(Keen, { | ||
debug: false, | ||
enabled: true, | ||
loaded: false, | ||
helpers: {}, | ||
utils: {}, | ||
version: '__VERSION__' | ||
}); | ||
Keen.log = function(message) { | ||
if (Keen.debug && typeof console == 'object') { | ||
console.log('[Keen IO]', message); | ||
K.prototype.writePath = function(str){ | ||
K.log('This method has been deprecated. Use client.url(\'events\') instead.'); | ||
if (!arguments.length) return this.config.writePath; | ||
if (!this.projectId()) { | ||
this.emit('error', 'Client instance is missing a projectId property'); | ||
return this.config.writePath || ('/3.0/projects/' + this.projectId() + '/events'); | ||
} | ||
this.config.writePath = str ? String(str) : ('/3.0/projects/' + this.projectId() + '/events'); | ||
return this; | ||
}; | ||
function serialize(data){ | ||
@@ -140,2 +189,2 @@ var query = []; | ||
module.exports = Keen; | ||
module.exports = K; |
@@ -24,3 +24,3 @@ var Keen = require('./index'); | ||
url = this.url(this.writePath() + '/' + encodeURIComponent(eventCollection)); | ||
url = this.url('events', encodeURIComponent(eventCollection)); | ||
data = {}; | ||
@@ -69,3 +69,3 @@ cb = callback; | ||
getRequestUrl = this.url(this.writePath() + '/' + encodeURIComponent(eventCollection), { | ||
getRequestUrl = this.url('events', encodeURIComponent(eventCollection), { | ||
api_key : this.writeKey(), | ||
@@ -120,3 +120,3 @@ data : base64.encode( json.stringify(extendedEventBody) ), | ||
url = this.url(this.writePath()); | ||
url = this.url('events'); | ||
cb = callback; | ||
@@ -123,0 +123,0 @@ callback = null; |
@@ -62,3 +62,3 @@ var Keen = require('./index'); | ||
sendEventData.call(this, '/' + encodeURIComponent(eventCollection), extendedEventBody, cb); | ||
sendEventData.call(this, encodeURIComponent(eventCollection), extendedEventBody, cb); | ||
cb = null; | ||
@@ -185,6 +185,11 @@ return this; | ||
function sendEventData(path, eventData, callback){ | ||
var data = JSON.stringify(eventData); | ||
var data = JSON.stringify(eventData), | ||
url; | ||
url = this.url('events', path); | ||
url = url.replace(this.config.protocol + '://' + this.config.host, ''); | ||
var options = { | ||
host: this.config.host, | ||
path: this.writePath() + path, | ||
path: url, | ||
method: 'POST', | ||
@@ -191,0 +196,0 @@ headers: { |
{ | ||
"name": "keen-tracking", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"main": "lib/server.js", | ||
@@ -5,0 +5,0 @@ "browser": "lib/browser.js", |
@@ -46,11 +46,48 @@ # keen-tracking.js [![Build Status](https://travis-ci.org/keen/keen-tracking.js.svg?branch=master)](https://travis-ci.org/keen/keen-tracking.js) | ||
**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. | ||
**Breaking change from keen-js:** the previous implementation of `client.url()` automatically included `https://api.keen.io/3.0/projects/PROJECT_ID` plus a `path` argument ('/events/whatever'). This design severely limited its utility, so we've revamped this method. | ||
You can also now pass in an object to append a serialized query string to the result, like so: | ||
This method now references an internal collection of resource paths, and constructs URLs using client configuration properties like `host` and `projectId`: | ||
```javascript | ||
var url = client.url('/3.0/projects', { key: 'value'} ); | ||
// https://api.keen.io/3.0/projects?key=value | ||
var url = client.url('projectId'); | ||
// Renders {protocol}://{host}/3.0/projects/{projectId} | ||
// Returns https://api.keen.io/3.0/projects/PROJECT_ID | ||
``` | ||
Default resources: | ||
* 'base': '`{protocol}`://`{host}`', | ||
* 'version': '`{protocol}`://`{host}`/3.0', | ||
* 'projects': '`{protocol}`://`{host}`/3.0/projects', | ||
* 'projectId': '`{protocol}`://`{host}`/3.0/projects/`{projectId}`', | ||
* 'events': '`{protocol}`://`{host}`/3.0/projects/`{projectId}`/events' | ||
Unmatching strings will be appended to the base resource, like so: | ||
```javascript | ||
var url = client.url('/3.0/projects'); | ||
// Returns https://api.keen.io/3.0/projects | ||
``` | ||
You can also pass in an object to append a serialized query string to the result, like so: | ||
```javascript | ||
var url = client.url('events', { api_key: 'YOUR_API_KEY' }); | ||
// Returns https://api.keen.io/3.0/projects/PROJECT_ID/events?api_key=YOUR_API_KEY | ||
``` | ||
Resources can be returned or added with the `client.resources()` method, like so: | ||
```javascript | ||
client.resources() | ||
// Returns client.config.resources object | ||
client.resources({ | ||
'new': '{protocol}://analytics.mydomain.com/my-custom-endpoint/{projectId}' | ||
}); | ||
client.url('new'); | ||
// Returns 'https://analytics.mydomain.com/my-custom-endpoint/PROJECT_ID' | ||
``` | ||
<a name="additional-resources"></a> | ||
@@ -90,3 +127,3 @@ **Additional resources:** | ||
Copy/paste this snippet of JavaScript above the </head> tag of your page to load the tracking library asynchronously. This technique sneaks the library into your page without significantly impacting page load speed. | ||
Copy/paste this snippet of JavaScript above the `</head>` tag of your page to load the tracking library asynchronously. This technique sneaks the library into your page without significantly impacting page load speed. | ||
@@ -98,3 +135,3 @@ ```html | ||
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.5.min.js',this); | ||
}('Keen','https://d26b395fwzu5fz.cloudfront.net/keen-tracking-0.1.0.min.js',this); | ||
@@ -101,0 +138,0 @@ // Executes when the library is loaded and ready |
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
83194
30
1631
931