When implementing Using the gplus api in Apps Script I noticed that there didn’t seem to be an easy way to just get the the number of plus ones given a URL – but yet I knew it must be possible because the g+ button includes the option to show a count. I’m currently working on Displaying analytics data on site pages and wanted to combine Google Analytics with Google Plus counts for that.
[ note: this does not work anymore as Google removed gadgets on sites]

Scraping the source

When you dig into the page source with a g+ button with a count enabled, you find this:

So all you have to do is find out how this is triggered. While researching I came across this blog post by Helmut Granda, and what you need is this

So now all that’s necessary is to scrape the web source to pull out the aggregateCount div. There’s not even any need for oAuth, as would probably be required if I was using the G+ api. I originally planned to use XMLService, but it’s a very trivial regex problem (and anyway it’s a hack) so I decided on using that instead. Here’s the code (I’m using Backing off on rate limiting since i’m going to be using this snippet in bulk and want to avoid rate limit problems)

Using sharedCount

As pointed out by fellow GDE Martin Hawksey, there is a sharedCount API for getting stuff like this. It also has the advantage of getting stats from facebook, linkedin etc. and hopefully will still work if Google change their button layout. The disadvantage is that it’s another API and another API key to worry about. It has a free plan of 10,000 a day which is fine for batch use cases like my one, but might run out of steam if using live on a popular web site.
I did a test on a few hundred pages and it came up with the same gPlus results (luckily). The sharedCount API was a bit slower on average, but was much more variable in performance.
ms gplus hack sharedCount
average 442.13 508.49
max 653 6271
min 322 22
Here’s the code for the sharedCount version (you’ll need to store your sharedCount apikey in your script properties and pass it through), and you can pick up the count from the GooglePlusOne property of the returned object.

Exceeded time limits

Sooner or later you’ll run into exceed quota problems if you are doing bulk analysis, so an option is to use cache. I use my own cachehandler library so I don’t need to worry about creating keys, but you can use the vanilla cache service if you want.
When I use this is in bulk mode, I schedule a pre-run whose only job is to populate cache. If it takes too long then just run it a few times. Then when I run my task that really does the processing all the plus one data is already in cache. Here’s the difference it makes to both the hack and the sharedCount version.
ms gplus hack sharedCount
average 41.95 44.78
max 150 141
min 9 10
So using cache pretty much equalizes the methods, but more importantly is 10 times as fast.
Here’s the cache version for each method. Both methods need a cache set up at the beginning


G+ hack

For more snippets like this see Google Apps Scripts snippets