2015-02-05 15:24:59 -08:00
// To use: set the range you want below, make sure your environment has the stripe key, then run:
2015-03-02 12:44:49 -08:00
// node scripts/analytics/subscriptionStats.js
2015-02-05 15:24:59 -08:00
require ( 'coffee-script' ) ;
require ( 'coffee-script/register' ) ;
2015-03-02 13:19:32 -08:00
_ = require ( 'lodash' ) ;
2015-02-05 15:24:59 -08:00
config = require ( '../../server_config' ) ;
if ( config . stripe . secretKey . indexOf ( 'sk_test_' ) == 0 ) {
throw new Error ( 'You should not run this on the test data... Get your environment in gear.' ) ;
}
stripe = require ( 'stripe' ) ( config . stripe . secretKey ) ;
var range = {
2015-10-22 06:41:54 -07:00
gt : '' + ( new Date ( '2015-09-01' ) . getTime ( ) / 1000 ) ,
lt : '' + ( new Date ( '2015-10-01' ) . getTime ( ) / 1000 )
2015-02-05 15:24:59 -08:00
} ;
2015-10-22 06:41:54 -07:00
var lastStartDate = null ;
2015-02-05 15:24:59 -08:00
begin = function ( starting _after ) {
var query = { date : range , limit : 100 } ;
if ( starting _after ) {
query . starting _after = starting _after ;
2015-10-22 06:41:54 -07:00
lastStartDate = starting _after ;
2015-02-05 15:24:59 -08:00
}
stripe . invoices . list ( query , onInvoicesReceived ) ;
}
2015-04-02 14:13:40 -07:00
customersPaid = [ ] ;
2015-02-05 15:24:59 -08:00
onInvoicesReceived = function ( err , invoices ) {
2015-10-22 06:41:54 -07:00
if ( err ) {
console . error ( "Got error fetching invoices:" , err ) ;
return begin ( lastStartDate ) ;
}
2015-02-05 15:24:59 -08:00
for ( var i in invoices . data ) {
var invoice = invoices . data [ i ] ;
if ( ! invoice . paid ) { continue ; }
2015-04-02 14:13:40 -07:00
if ( ! invoice . total ) { continue ; } // not paying anything!
//console.log(invoice);
2015-02-05 15:24:59 -08:00
customersPaid . push ( invoice . customer ) ;
}
if ( invoices . has _more ) {
console . log ( 'Loaded' , customersPaid . length , 'invoices.' )
begin ( invoices . data [ i ] . id ) ;
}
else {
2015-04-02 14:13:40 -07:00
console . log ( '--- Actual active total customers:' , _ . unique ( customersPaid ) . length ) ;
2015-02-05 15:24:59 -08:00
loadNewCustomers ( ) ;
}
2015-04-02 14:13:40 -07:00
} ;
2015-02-05 15:24:59 -08:00
loadNewCustomers = function ( starting _after ) {
query = { created : range , limit : 100 } ;
if ( starting _after ) {
query . starting _after = starting _after ;
2015-10-22 06:41:54 -07:00
lastStartDate = starting _after ;
2015-02-05 15:24:59 -08:00
}
stripe . customers . list ( query , onCustomersReceived ) ;
2015-04-02 14:13:40 -07:00
} ;
2015-02-05 15:24:59 -08:00
newCustomersPaid = [ ] ;
onCustomersReceived = function ( err , customers ) {
2015-10-22 06:41:54 -07:00
if ( err ) {
console . error ( "Got error fetching customers:" , err ) ;
return loadNewCustomers ( lastStartDate ) ;
}
2015-02-05 15:24:59 -08:00
for ( var i in customers . data ) {
var customer = customers . data [ i ] ;
if ( customersPaid . indexOf ( customer . id ) == - 1 ) { continue ; }
newCustomersPaid . push ( customer . id ) ;
}
if ( customers . has _more ) {
console . log ( 'Loaded' , newCustomersPaid . length , 'new customers.' ) ;
loadNewCustomers ( customers . data [ i ] . id ) ;
}
else {
2015-04-02 14:13:40 -07:00
console . log ( '--- Actual new customers:' , newCustomersPaid . length ) ;
2015-02-05 15:24:59 -08:00
}
2015-04-02 14:13:40 -07:00
} ;
2015-02-05 15:24:59 -08:00
2015-10-22 06:41:54 -07:00
begin ( ) ;