Client = function (token) { console.warn("The function `Client(token)` is deprecated. It is now called `init(token)`."); return create_client(token); }
...
0.0.6 / 2012-01-01
===================
* added engage support
* people.set()
* people.increment()
* people.delete_user()
* deprecated old constructor: require("mixpanel").Client(token)
* added new constructor: require("mixpanel").init(token)
...
init = function (token, config) {
var metrics = {};
var proxyAgent = create_proxy_agent();
// mixpanel constants
var MAX_BATCH_SIZE = 50,
TRACK_AGE_LIMIT = 60 * 60 * 24 * 5;
if(!token) {
throw new Error("The Mixpanel Client needs a Mixpanel token: `init(token)`");
}
// Default config
metrics.config = {
test: false,
debug: false,
verbose: false,
host: 'api.mixpanel.com',
protocol: 'http'
};
metrics.token = token;
/**
* sends an async GET or POST request to mixpanel
* for batch processes data must be send in the body of a POST
* @param {object} options
* @param {string} options.endpoint
* @param {object} options.data the data to send in the request
* @param {string} [options.method] e.g. `get` or `post`, defaults to `get`
* @param {function} callback called on request completion or error
*/
metrics.send_request = function(options, callback) {
callback = callback || function() {};
var content = (new Buffer(JSON.stringify(options.data))).toString('base64'),
endpoint = options.endpoint,
method = (options.method || 'GET').toUpperCase(),
query_params = {
'ip': 0,
'verbose': metrics.config.verbose ? 1 : 0
},
key = metrics.config.key,
request_lib = REQUEST_LIBS[metrics.config.protocol],
request_options = {
host: metrics.config.host,
port: metrics.config.port,
headers: {},
method: method
},
request;
if (!request_lib) {
throw new Error(
"Mixpanel Initialization Error: Unsupported protocol " + metrics.config.protocol + ". " +
"Supported protocols are: " + Object.keys(REQUEST_LIBS)
);
}
if (method === 'POST') {
content = 'data=' + content;
request_options.headers['Content-Type'] = 'application/x-www-form-urlencoded';
request_options.headers['Content-Length'] = Buffer.byteLength(content);
} else if (method === 'GET') {
query_params.data = content;
}
// add `key` query params
if (key) {
query_params.api_key = key;
} else if (endpoint === '/import') {
throw new Error("The Mixpanel Client needs a Mixpanel api key when importing old events: `init(token, { key: ... })`");
}
if (proxyAgent) {
request_options.agent = proxyAgent;
}
if (metrics.config.test) {
query_params.test = 1;
}
request_options.path = endpoint + "?" + querystring.stringify(query_params);
request = request_lib.request(request_options, function(res) {
var data = "";
res.on('data', function(chunk) {
data += chunk;
});
res.on('end', function() {
var e;
if (metrics.config.verbose) {
try {
var result = JSON.parse(data);
if(result.status != 1) {
e = new Error("Mixpanel Server Error: " + result.error);
}
}
catch(ex) {
e = new Error("Could not parse response from Mixpanel");
}
}
else {
e = (data !== '1') ? new Error("Mixpanel Server Error: " + data) : undefined;
}
callback(e);
});
});
request.on('error', function(e) {
if (metrics.config.debug) {
console.log("Got Error: " + e.message);
}
callback(e);
});
if (method === 'POST') {
request.write(content);
}
request.end();
};
/**
* Send an event to Mixpanel, using th ...
...
// grab the Mixpanel factory
var Mixpanel = require('./lib/mixpanel-node');
// create an instance of the mixpanel client
var mixpanel = Mixpanel.init('962dbca1bbc54701d402c94d65b4a20e');
mixpanel.set_config({ debug: true });
// track an event with optional properties
mixpanel.track("my event", {
distinct_id: "some unique client id",
as: "many",
properties: "as",
...
async_all = function (requests, handler, callback) { var total = requests.length, errors = null, results = [], done = function (err, result) { if (err) { // errors are `null` unless there is an error, which allows for promisification errors = errors || []; errors.push(err); } results.push(result); if (--total === 0) { callback(errors, results) } }; if (total === 0) { callback(errors, results); } else { for(var i = 0, l = requests.length; i < l; i++) { handler(requests[i], done); } } }
n/a