NodeJS AdMob API

I was working on a script that would pull the total earnings between all my ad networks. Below is a NodeJS script to display your total earnings for the day between all your sites/apps. Only 3 variables need to be configured for it to work.

//NodeJS Get Admob Todays Earnings
//http://Beyondem.com
var request = require('request');

//Configure These
var apikey = ""; //Tools->API
var apipassword = ""; //Not your actual password, get the read only password: Account->Account Information
var apiemail = ""; //AdMob Email

//Leave these empty:
var apitoken = "";
var siteslist = ""; 

function getLoginToken(){
request.post(
    'https://api.admob.com/v2/auth/login',
    {form:{ 'client_key': apikey, 'email': apiemail, 'password': apipassword} },
    function (error, response, body) {
        if (!error && response.statusCode == 200) {
        	body = JSON.parse(body);
        	apitoken = body.data.token;
            console.log("Got Login Token:" + apitoken);
            getSites();
        }
    }
);

}

function getToday(){
	var currentDate = new Date()
  var day = currentDate.getDate();
  var month = currentDate.getMonth() + 1;
  var year = currentDate.getFullYear();
  if (month.toString().length == 1){
  	month = "0" + month;
  }
  var today = year + "-" + month + "-" + day;
return today;
}

function getStats(){
var today = getToday();
	var url = "http://api.admob.com/v2/site/stats?client_key=" + apikey + "&token=" + apitoken + "&start_date=" + today + "&end_date=" + today + siteslist;
	//console.log("getStats() url: " + url);
	request(url, function(error, response, body) {
	//	console.log("getStats() body:" + body);
		body = JSON.parse(body);
  console.log("getStats():" + JSON.stringify(body.data[0].revenue));
});

}

function getSites(){
if (apitoken != ""){
var url = "http://api.admob.com/v2/site/search?client_key=" + apikey + "&token=" + apitoken;
request(url, function(error, response, body) {
	body = JSON.parse(body);
	if (body.data.length > 0){
siteslist = "";
for (var i = 0; i < body.data.length; i++){
siteslist = siteslist + "&site_id[]=" + body.data[i].id;
}
console.log("Siteslist: " + siteslist);
getStats();
}else{
	console.log("No Sites Found");
}
});
}else{
	console.log("getSites(): No API Token");
}
}

getLoginToken();
//getSites();
//getDetails();

Leave a Reply