add mixpanel

This commit is contained in:
typpo 2012-04-07 16:13:02 -04:00
parent 6494478c60
commit 80691d1228
3 changed files with 23 additions and 8 deletions

23
app.js
View File

@ -3,6 +3,7 @@ var express = require('express')
, nodemailer = require('nodemailer') , nodemailer = require('nodemailer')
, _ = require('underscore') , _ = require('underscore')
, fs = require('fs') , fs = require('fs')
, mixpanel = require('mixpanel')
/* /*
, amazonses = require('amazon-ses') , amazonses = require('amazon-ses')
*/ */
@ -19,6 +20,7 @@ var sendgrid = new SendGrid(
process.env.SENDGRID_PASSWORD || 'd0y4yjqn' process.env.SENDGRID_PASSWORD || 'd0y4yjqn'
) )
*/ */
var mpq = new mixpanel.Client('6e6e6b71ed5ada4504c52d915388d73d');
var redis; var redis;
if (process.env.NODE_ENV == 'production') if (process.env.NODE_ENV == 'production')
@ -44,6 +46,8 @@ app.get('/', function(req, res) {
}); });
app.post('/text', function(req, res) { app.post('/text', function(req, res) {
mpq.track('text',
{number: req.body.number, message: req.body.message, ip: req.connection.remoteAddress});
var number = stripPhone(req.body.number); var number = stripPhone(req.body.number);
if (number.length < 9 || number.length > 10) { if (number.length < 9 || number.length > 10) {
@ -56,6 +60,7 @@ app.post('/text', function(req, res) {
redis.incr(phonekey, function(err, num) { redis.incr(phonekey, function(err, num) {
if (err) { if (err) {
mpq.track('redis fail');
res.send({success:false,message:'Could not validate phone# quota.'}); res.send({success:false,message:'Could not validate phone# quota.'});
return; return;
} }
@ -63,11 +68,13 @@ app.post('/text', function(req, res) {
setTimeout(function() { setTimeout(function() {
redis.decr(phonekey, function(err, num) { redis.decr(phonekey, function(err, num) {
if (err) { if (err) {
mpq.track('failed to decr phone quota', {number: number});
console.log('*** WARNING failed to decr ' + number); console.log('*** WARNING failed to decr ' + number);
} }
}); });
}, 1000*60*3); }, 1000*60*3);
if (num > 3) { if (num > 3) {
mpq.track('exceeded phone quota');
res.send({success:false,message:'Exceeded quota for this phone number.'}); res.send({success:false,message:'Exceeded quota for this phone number.'});
return; return;
} }
@ -75,19 +82,25 @@ app.post('/text', function(req, res) {
// now check against ip quota // now check against ip quota
redis.incr(ipkey, function(err, num) { redis.incr(ipkey, function(err, num) {
if (err) { if (err) {
mpq.track('redis fail');
res.send({success:false,message:'Could not validate IP quota.'}); res.send({success:false,message:'Could not validate IP quota.'});
return; return;
} }
if (num > 500) { if (num > 500) {
mpq.track('exceeded ip quota');
res.send({success:false,message:'Exceeded quota for this IP address.'}); res.send({success:false,message:'Exceeded quota for this IP address.'});
return; return;
} }
sendText(req.body.number, req.body.message, function(err) { sendText(req.body.number, req.body.message, function(err) {
if (err) if (err) {
mpq.track('sendText failed');
res.send({success:false,message:'Communication with SMS gateway failed.'}); res.send({success:false,message:'Communication with SMS gateway failed.'});
else }
else {
mpq.track('sendText success');
res.send({success:true}); res.send({success:true});
}
}); });
}); });
@ -123,16 +136,14 @@ function sendText(phone, message, cb) {
_.each(providers, function(provider) { _.each(providers, function(provider) {
var email = provider.replace('%s', phone); var email = provider.replace('%s', phone);
console.log('email', email);
var child = spawn('sendmail', ['-f', 'txt@textbelt.com', email]); var child = spawn('sendmail', ['-f', 'txt@textbelt.com', email]);
child.stdout.on('data', console.log); child.stdout.on('data', console.log);
child.stderr.on('data', console.log); child.stderr.on('data', console.log);
child.on('error', function() { child.on('error', function(data) {
console.log('failed', email); mpq.track('sendmail failed', {email: email, data: data});
done(); done();
}); });
child.on('exit', function(code, signal) { child.on('exit', function(code, signal) {
console.log('done', email);
done(); done();
}); });
child.stdin.write(message + '\n.'); child.stdin.write(message + '\n.');

View File

@ -8,6 +8,7 @@
, "amazon-ses": "latest" , "amazon-ses": "latest"
, "sendgrid": "latest" , "sendgrid": "latest"
, "underscore": "latest" , "underscore": "latest"
, "mixpanel": "latest"
} }
, "devDependencies": { , "devDependencies": {
} }

View File

@ -3,7 +3,7 @@
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="chrome=1"> <meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>TextBelt by typpo</title> <title>TextBelt - Free Texting API</title>
<link rel="stylesheet" href="http://typpo.github.com/textbelt/stylesheets/styles.css"> <link rel="stylesheet" href="http://typpo.github.com/textbelt/stylesheets/styles.css">
<link rel="stylesheet" href="http://typpo.github.com/textbelt/stylesheets/pygment_trac.css"> <link rel="stylesheet" href="http://typpo.github.com/textbelt/stylesheets/pygment_trac.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
@ -15,7 +15,10 @@
<link rel="stylesheet" href="stylesheets/ie.css"> <link rel="stylesheet" href="stylesheets/ie.css">
<![endif]--> <![endif]-->
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<!-- start Mixpanel --><script type="text/javascript">(function(d,c){var a,b,g,e;a=d.createElement("script");a.type="text/javascript";a.async=!0;a.src=("https:"===d.location.protocol?"https:":"http:")+'//api.mixpanel.com/site_media/js/api/mixpanel.2.js';b=d.getElementsByTagName("script")[0];b.parentNode.insertBefore(a,b);c._i=[];c.init=function(a,d,f){var b=c;"undefined"!==typeof f?b=c[f]=[]:f="mixpanel";g="disable track track_pageview track_links track_forms register register_once unregister identify name_tag set_config".split(" ");
for(e=0;e<g.length;e++)(function(a){b[a]=function(){b.push([a].concat(Array.prototype.slice.call(arguments,0)))}})(g[e]);c._i.push([a,d,f])};window.mixpanel=c})(document,[]);
mixpanel.init("6e6e6b71ed5ada4504c52d915388d73d");</script><!-- end Mixpanel -->
<script>mixpanel.track('main');</script>
</head> </head>
<body> <body>