Comparing version 0.0.5 to 0.0.6
@@ -5,3 +5,5 @@ /* | ||
Modifications by Carl Sverre | ||
Copyright (c) 2012 Carl Sverre | ||
Released under the MIT license. | ||
*/ | ||
@@ -13,3 +15,3 @@ | ||
var client = function(token) { | ||
var create_client = function(token, config) { | ||
var metrics = {}; | ||
@@ -23,4 +25,3 @@ | ||
test: false, | ||
debug: false, | ||
endpoint_path: "/track" | ||
debug: false | ||
}; | ||
@@ -44,3 +45,3 @@ | ||
*/ | ||
metrics.send_request = function(data, callback) { | ||
metrics.send_request = function(endpoint, data, callback) { | ||
callback = callback || function() {}; | ||
@@ -61,6 +62,5 @@ var event_data = new Buffer(JSON.stringify(data)); | ||
var url = metrics.config.endpoint_path; | ||
var query = querystring.stringify(request_data); | ||
request_options.path = [url,"?",query].join(""); | ||
request_options.path = [endpoint,"?",query].join(""); | ||
@@ -96,6 +96,10 @@ http.get(request_options, function(res) { | ||
metrics.track = function(event, properties, callback) { | ||
if (!properties) { properties = {}; } | ||
if (!properties.token) { properties.token = metrics.token; } | ||
if (!properties.time) { properties.time = get_unixtime(); } | ||
if (typeof(properties) === 'function' || !properties) { | ||
callback = properties; | ||
properties = {}; | ||
} | ||
properties.token = metrics.token; | ||
properties.time = get_unixtime(); | ||
var data = { | ||
@@ -111,28 +115,123 @@ 'event' : event, | ||
metrics.send_request(data,callback); | ||
metrics.send_request('/track', data, callback); | ||
}; | ||
/** | ||
track_funnel(funnel, step, goal, properties, callback) | ||
--- | ||
this function tracks a specific step in a funnel | ||
metrics.people = { | ||
/** | ||
people.set(distinct_id, prop, to, callback) | ||
--- | ||
set properties on an user record in engage | ||
NOTE: this is not the recommended way of using funnels, use events | ||
and the funnel creator in the web interface instead | ||
usage: | ||
mixpanel.people.set('bob', 'gender', 'm'); | ||
mixpanel.people.set('joe', { | ||
'company': 'acme', | ||
'plan': 'premium' | ||
}); | ||
*/ | ||
set: function(distinct_id, prop, to, callback) { | ||
var $set = {}, data = {}; | ||
if (typeof(prop) === 'object') { | ||
callback = to; | ||
$set = prop; | ||
} else { | ||
$set[prop] = to; | ||
} | ||
var data = { | ||
'$set': $set, | ||
'$token': metrics.token, | ||
'$distinct_id': distinct_id | ||
} | ||
if(metrics.config.debug) { | ||
console.log("Sending the following data to Mixpanel (Engage):"); | ||
console.log(data); | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
}, | ||
/** | ||
people.increment(distinct_id, prop, to, callback) | ||
--- | ||
increment/decrement properties on an user record in engage | ||
funnel:string the funnel name | ||
step:int the step number | ||
goal:string the name of the step | ||
properties:object additional event properties to send | ||
callback:function(err:Error) callback is called when the request is | ||
finished or an error occurs | ||
*/ | ||
metrics.track_funnel = function(funnel, step, goal, properties, callback) { | ||
if(!properties) { properties = {}; } | ||
usage: | ||
mixpanel.people.increment('bob', 'page_views', 1); | ||
// or, for convenience, if you're just incrementing a counter by 1, you can | ||
// simply do | ||
mixpanel.people.increment('bob', 'page_views'); | ||
// to decrement a counter, pass a negative number | ||
mixpanel.people.increment('bob', 'credits_left', -1); | ||
// like mixpanel.people.set(), you can increment multiple properties at once: | ||
mixpanel.people.increment('bob', { | ||
counter1: 1, | ||
counter2: 3, | ||
counter3: -2 | ||
}); | ||
*/ | ||
increment: function(distinct_id, prop, by, callback) { | ||
var $add = {}, data = {}; | ||
if (typeof(prop) === 'object') { | ||
callback = by; | ||
Object.keys(prop).forEach(function(key) { | ||
var val = prop[key]; | ||
if (isNaN(parseFloat(val))) { | ||
console.error("Invalid increment value passed to mixpanel.people.increment - must be a number"); | ||
return; | ||
} else { | ||
$add[key] = val; | ||
} | ||
}); | ||
} else { | ||
if (!by) { by = 1; } | ||
$add[prop] = by; | ||
} | ||
var data = { | ||
'$add': $add, | ||
'$token': metrics.token, | ||
'$distinct_id': distinct_id | ||
} | ||
if(metrics.config.debug) { | ||
console.log("Sending the following data to Mixpanel (Engage):"); | ||
console.log(data); | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
}, | ||
/** | ||
people.delete_user(distinct_id, callback) | ||
--- | ||
delete an user record in engage | ||
properties.funnel = funnel; | ||
properties.step = step; | ||
properties.goal = goal; | ||
metrics.track('mp_funnel', properties, callback); | ||
usage: | ||
mixpanel.people.delete_user('bob'); | ||
*/ | ||
delete_user: function(distinct_id, callback) { | ||
var data = { | ||
'$delete': distinct_id, | ||
'$token': metrics.token, | ||
'$distinct_id': distinct_id | ||
}; | ||
if(metrics.config.debug) { | ||
console.log("Deleting the user from engage:", distinct_id); | ||
} | ||
metrics.send_request('/engage', data, callback); | ||
} | ||
}; | ||
@@ -155,2 +254,6 @@ | ||
}; | ||
if (config) { | ||
metrics.set_config(config); | ||
} | ||
@@ -162,3 +265,7 @@ return metrics; | ||
module.exports = { | ||
Client: client | ||
Client: function(token) { | ||
console.warn("The function `Client(token)` is deprecated. It is now called `init(token)`."); | ||
return create_client(token); | ||
}, | ||
init: create_client | ||
}; |
@@ -5,5 +5,5 @@ { | ||
"keywords": ["mixpanel", "analytics", "api", "stats"], | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"homepage": "https://github.com/carlsverre/mixpanel-node", | ||
"author": "Carl Sverre (http://carlsverre.com)", | ||
"author": "Carl Sverre", | ||
"main": "lib/mixpanel-node", | ||
@@ -10,0 +10,0 @@ "directories": { |
@@ -9,32 +9,51 @@ Mixpanel-node | ||
npm install mixpanel | ||
npm install mixpanel | ||
Usage | ||
Quick Start | ||
----- | ||
var mixpanel = require('mixpanel'); | ||
// grab the Mixpanel factory | ||
var Mixpanel = require('../lib/mixpanel-node'); | ||
var mp_client = new mixpanel.Client('YOUR MIXPANEL TOKEN'); | ||
// create an instance of the mixpanel client | ||
var mixpanel = Mixpanel.init('6fd9434dba686db2d1ab66b4462a3a67'); | ||
mp_client.track("my event", { | ||
distinct_id: "some unique client id", | ||
as: "many", | ||
properties: "as", | ||
you: "want" | ||
}, function(err) { | ||
if(err) throw err; | ||
}); | ||
// manual funnel tracking is supported, but not recommended | ||
mp_client.track_funnel("my funnel", 1, "first goal", { | ||
distinct_id: "unique identifier" | ||
}, function(err) { | ||
if(err) throw err; | ||
}); | ||
mp_client.track_funnel("my funnel", 2, "second goal", { | ||
distinct_id: "unique identifier" | ||
}, function(err) { | ||
if(err) throw err; | ||
}); | ||
// track an event with optional properties | ||
mixpanel.track("my event", { | ||
distinct_id: "some unique client id", | ||
as: "many", | ||
properties: "as", | ||
you: "want" | ||
}); | ||
mixpanel.track("played_game"); | ||
// create or update a user in Mixpanel Engage | ||
mixpanel.people.set("billybob", { | ||
$first_name: "Billy", | ||
$last_name: "Bob", | ||
plan: "premium", | ||
games_played: 1, | ||
points: 0 | ||
}); | ||
// set a single property on a user | ||
mixpanel.people.set("billybob", "plan", "free"); | ||
// increment a numeric property | ||
mixpanel.people.increment("billybob", "games_played"); | ||
// increment a numeric property by a different amount | ||
mixpanel.people.increment("billybob", "points", 15); | ||
// increment multiple properties | ||
mixpanel.people.increment("billybob", {"points": 10, "games_played": | ||
1}); | ||
// delete a user | ||
mixpanel.people.delete_user("billybob"); | ||
// all functions that send data to mixpanel take an optional | ||
// callback as the last argument | ||
mixpanel.track("test", function(err) { if (err) throw err; }); | ||
Attribution/Credits | ||
@@ -46,2 +65,8 @@ ------------------- | ||
Modifications by Carl Sverre | ||
Copyright (c) 2012 Carl Sverre | ||
License | ||
------------------- | ||
Released under the MIT license. See file called LICENSE for more | ||
details. |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
12392
0
246
71
0
5
3