// ==UserScript==
// @name			Social Media Metrics Plugin for Google Anayltics
// @namespace		smmDataInGA
// @include			https://www.google.com/analytics/reporting/content_detail?*
// @version			1.1
// @description		This will display social media marketing data in google analytics
// ==/UserScript==

var smmDataInGA = {};

// 
try{

smmDataInGA.pageURL = "";
smmDataInGA.pageURL = document.getElementById('ControlBar').getElementsByTagName('a')[0].href;
smmDataInGA.gaPageSparkLine = document.getElementById('PageviewsSparkline').parentNode.parentNode.parentNode;

}
catch(e){};


// digg
try{

var addDiggToGA = {};
addDiggToGA.numberOfDiggs = "?";
addDiggToGA.tempDiv = document.createElement('div');
addDiggToGA.tempDiv.style.display = "none";
addDiggToGA.newDisplayDiv = document.createElement('div');

// get digg count and display it
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://digg.com/tools/diggthis.php?u='+smmDataInGA.pageURL,
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(responseDetails) {

	addDiggToGA.tempDiv.innerHTML = responseDetails.responseText.match(/<li class="digg-count"[^>]*>([^<]|<[^\/]|<\/[^l]|<\/l[^i]|<\/li\s*[^>])*<\/li\s*>/);

	addDiggToGA.link = addDiggToGA.tempDiv.getElementsByTagName('a')[0];
	addDiggToGA.link = addDiggToGA.link.href.replace(/https?:\/\/[^\/]*/i,"http://digg.com");
	
	// START: Get the number of diggs
	addDiggToGA.numberOfDiggs = addDiggToGA.tempDiv.getElementsByTagName('strong')[0].innerHTML;
	if( addDiggToGA.numberOfDiggs == "Digg" ){
		addDiggToGA.numberOfDiggs = 0;
	}
	else{
		addDiggToGA.numberOfDiggs = addDiggToGA.numberOfDiggs.match(/\d*/);

		if( addDiggToGA.numberOfDiggs == "" ){
			addDiggToGA.numberOfDiggs = 0;
		}
	}
	// END: Get the number of diggs
		
	addDiggToGA.newDisplayDiv.innerHTML = "<div class='sparkline'>"+
					    "<div>"+
					      "<div class='visualization'>"+
					      "<a title='Digg it!' target='_blank' href='"+addDiggToGA.link+"'>"+
						"<img src='http://erikvold.com/images/external/digg-logo-small.png' border='0' /></a>"+
					      "</div>"+
					    "</div>"+
					    "<div>"+
					      "<div class='statistic'>"+
						  "<a title='Digg it!' target='_blank' href='"+addDiggToGA.link+"'>"+
						"<h3>"+
						  "<span class='primary_value'>"+
						  addDiggToGA.numberOfDiggs+
						  "&nbsp;</span>"+
						  "<span class='label'>&nbsp;Total&nbsp;Digg(s)</span>"+
						"</h3></a>"+
					      "</div>"+
					    "</div>"+
					  "</div>";
					
	
			smmDataInGA.gaPageSparkLine.appendChild( addDiggToGA.newDisplayDiv );
    }
});

}
catch(e){};


// sphinn
try{

	var addSphinnToGA = {};
	addSphinnToGA.numberOfSphinns = "?";
	
	addSphinnToGA.tempDiv = document.createElement('div');
	addSphinnToGA.tempDiv.style.display = "none";
	addSphinnToGA.newDisplayDiv = document.createElement('div');


// get sphinn count and display it
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://sphinn.com/evb/url.php?url='+smmDataInGA.pageURL,
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(responseDetails) {

	addSphinnToGA.tempDiv.innerHTML = responseDetails.responseText.match(/<ul class="wrap"[^>]*>([^<]|<[^\/]|<\/[^u]|<\/u[^l]|<\/ul\s*[^>])*<\/ul\s*>/);

	addSphinnToGA.link = addSphinnToGA.tempDiv.getElementsByTagName('a')[0].href;

	addSphinnToGA.numberOfSphinns = addSphinnToGA.tempDiv.getElementsByTagName('b')[0].innerHTML;

	if( addSphinnToGA.numberOfSphinns == "Sphinn it!" ){
		addSphinnToGA.numberOfSphinns = 0;
	}
	else{
		addSphinnToGA.numberOfSphinns = addSphinnToGA.numberOfSphinns.match(/\d*/);

		if( addSphinnToGA.numberOfSphinns == "" ){
			addSphinnToGA.numberOfSphinns = 0;
		}
	}
		
	addSphinnToGA.newDisplayDiv.innerHTML = "<div class='sparkline'>"+
					    "<div>"+
					      "<div class='visualization'>"+
					      "<a title='Sphinn it!' target='_blank' href='"+addSphinnToGA.link+"'>"+
						"<img src='http://erikvold.com/images/external/sphinn-logo-sm.jpg' border='0' /></a>"+
					      "</div>"+
					    "</div>"+
					    "<div>"+
					      "<div class='statistic'>"+
						  "<a title='Sphinn it!' target='_blank' href='"+addSphinnToGA.link+"'>"+
						"<h3>"+
						  "<span class='primary_value'>"+
						  addSphinnToGA.numberOfSphinns+
						  "&nbsp;</span>"+
						  "<span class='label'>&nbsp;Total&nbsp;Sphinn(s)</span>"+
						"</h3></a>"+
					      "</div>"+
					    "</div>"+
					  "</div>";;
					
	
			smmDataInGA.gaPageSparkLine.appendChild( addSphinnToGA.newDisplayDiv );
    }
});
}
catch(e){};



// del.icio.us
try{

	var addDeliciousToGA = {};
	addDeliciousToGA.numberOfSaves = "?";
	
	addDeliciousToGA.tempDiv = document.createElement('div');
	addDeliciousToGA.tempDiv.style.display = "none";
	addDeliciousToGA.newDisplayDiv = document.createElement('div');


// get del.icio.us save count and display it
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://badges.del.icio.us/feeds/json/url/data?url='+smmDataInGA.pageURL,
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(responseDetails) {
    	
	addDeliciousToGA.tempDiv.innerHTML = responseDetails.responseText.match(/"total_posts":\d+/);
	
	addDeliciousToGA.numberOfSaves = addDeliciousToGA.tempDiv.innerHTML.match(/\d+/);
	
	if(addDeliciousToGA.numberOfSaves == '' || addDeliciousToGA.numberOfSaves == null){
		addDeliciousToGA.numberOfSaves = 0;
	}
		
	addDeliciousToGA.newDisplayDiv.innerHTML = "<div class='sparkline'>"+
					    "<div>"+
					      "<div class='visualization'>"+
					      "<a title='Bookmark it!' target='_blank' href='http://delicious.com/save?url="+smmDataInGA.pageURL+"'>"+
						"<img src='http://erikvold.com/images/external/delicious-logo-small.jpg' border='0' /></a>"+
					      "</div>"+
					    "</div>"+
					    "<div>"+
					      "<div class='statistic'>"+
						  "<a title='Bookmark it!' target='_blank' href='http://delicious.com/save?url="+smmDataInGA.pageURL+"'>"+
						"<h3>"+
						  "<span class='primary_value'>"+
						  addDeliciousToGA.numberOfSaves+
						  "&nbsp;</span>"+
						  "<span class='label'>&nbsp;Total&nbsp;Save(s)</span>"+
						"</h3></a>"+
					      "</div>"+
					    "</div>"+
					  "</div>";;
					
	
			smmDataInGA.gaPageSparkLine.appendChild( addDeliciousToGA.newDisplayDiv );
    }
});
}
catch(e){};


// Yahoo.ca Site Explorer Inlinks
try{

	var addYahooInlinksToGA = {};
	addYahooInlinksToGA.numberOfInlinks = "?";
	
	addYahooInlinksToGA.tempDiv = document.createElement('div');
	addYahooInlinksToGA.tempDiv.style.display = "none";
	addYahooInlinksToGA.newDisplayDiv = document.createElement('div');


// get yahoo inlink count and display it
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://ca.siteexplorer.search.yahoo.com/siteexplorer/search?p='+smmDataInGA.pageURL+"&bwm=i&bwmo=d&bwmf=u",
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(responseDetails) {
    	
	addYahooInlinksToGA.tempDiv.innerHTML = responseDetails.responseText.match(/Inlinks \([\d,]+\)/i);
	
	addYahooInlinksToGA.numberOfInlinks = addYahooInlinksToGA.tempDiv.innerHTML.match(/[\d,]+/);
	
	if(addYahooInlinksToGA.numberOfInlinks == '' || addYahooInlinksToGA.numberOfInlinks == null){
		addYahooInlinksToGA.numberOfInlinks = 0;
	}
		
	addYahooInlinksToGA.newDisplayDiv.innerHTML = "<div class='sparkline'>"+
					    "<div>"+
					      "<div class='visualization'>"+
					      "<a title='View Yahoo.ca Inlinks Report' target='_blank' href='http://ca.siteexplorer.search.yahoo.com/siteexplorer/search?p="+smmDataInGA.pageURL+"&bwm=i&bwmo=d&bwmf=u'>"+
						"<img src='http://erikvold.com/images/external/se-ylogo.png' border='0' /></a>"+
					      "</div>"+
					    "</div>"+
					    "<div>"+
					      "<div class='statistic'>"+
						  "<a title='View Yahoo.ca Inlinks Report' target='_blank' href='http://ca.siteexplorer.search.yahoo.com/siteexplorer/search?p="+smmDataInGA.pageURL+"&bwm=i&bwmo=d&bwmf=u'>"+
						"<h3>"+
						  "<span class='primary_value'>"+
						  addYahooInlinksToGA.numberOfInlinks+
						  "&nbsp;</span>"+
						  "<span class='label'>&nbsp;Total&nbsp;Inlink(s)</span>"+
						"</h3></a>"+
					      "</div>"+
					    "</div>"+
					  "</div>";;
					
	
			smmDataInGA.gaPageSparkLine.appendChild( addYahooInlinksToGA.newDisplayDiv );
    }
});
}
catch(e){};


// StumbleUpon Reviews
try{

	var addStumbleUponReviewsToGA = {};
	addStumbleUponReviewsToGA.numberOfReviews = "?";
	
	addStumbleUponReviewsToGA.tempDiv = document.createElement('div');
	addStumbleUponReviewsToGA.tempDiv.style.display = "none";
	addStumbleUponReviewsToGA.newDisplayDiv = document.createElement('div');


// get stumbleupon review count and display it
GM_xmlhttpRequest({
    method: 'GET',
    url: 'http://www.stumbleupon.com/url/'+smmDataInGA.pageURL.replace(/^https?:\/\//i,""),
    headers: {
        'User-agent': 'Mozilla/4.0 (compatible) Greasemonkey',
        'Accept': 'application/atom+xml,application/xml,text/xml',
    },
    onload: function(responseDetails) {
    	
	addStumbleUponReviewsToGA.numberOfReviews = responseDetails.responseText.match(/<span\s+class="textOk"\s*>\s*<strong>\s*[\d,]+s*<\/strong>\s*<\/span>\s*reviews/i);
	
	if(addStumbleUponReviewsToGA.numberOfReviews == '' || addStumbleUponReviewsToGA.numberOfReviews == null){
		addStumbleUponReviewsToGA.numberOfReviews = "0";
	}
	
	addStumbleUponReviewsToGA.numberOfReviews = addStumbleUponReviewsToGA.numberOfReviews.toString().match(/[\d,]+/);
		
	addStumbleUponReviewsToGA.newDisplayDiv.innerHTML = "<div class='sparkline'>"+
					    "<div>"+
					      "<div class='visualization'>"+
					      "<a title='View StumbleUpon Review(s)' target='_blank' href='http://www.stumbleupon.com/url/"+smmDataInGA.pageURL.replace(/^https?:\/\//i,"")+"'>"+
						"<img src='http://erikvold.com/images/external/stumbleupon-logo-sm.jpg' border='0' /></a>"+
					      "</div>"+
					    "</div>"+
					    "<div>"+
					      "<div class='statistic'>"+
						  "<a title='View StumbleUpon Review(s)' target='_blank' href='http://www.stumbleupon.com/url/"+smmDataInGA.pageURL.replace(/^https?:\/\//i,"")+"'>"+
						"<h3>"+
						  "<span class='primary_value'>"+
						  addStumbleUponReviewsToGA.numberOfReviews+
						  "&nbsp;</span>"+
						  "<span class='label'>&nbsp;Total&nbsp;Review(s)</span>"+
						"</h3></a>"+
					      "</div>"+
					    "</div>"+
					  "</div>";
					
	
			smmDataInGA.gaPageSparkLine.appendChild( addStumbleUponReviewsToGA.newDisplayDiv );
    }
});
}
catch(e){};