How do I get the counter of a google plus +1 button?
Asked Answered
E

3

25

I have added a google +1 button to a website, but I want to get it's counter so i can do some math over it. is it possible to enter the iframe created by the standard method of creating the +1 button or do I need to make some adjustment?

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone></g:plusone>

I've tried this link:1 , but this is not very accurate

Eldreda answered 13/9, 2011 at 14:29 Comment(0)
G
26

If you can access curl/file_get_contents/readfile/wget or some way to fetch an external URL, this is quite simple.

Load the following URL: https://plusone.google.com/_/+1/fastbutton?url=URLENCODED_URI (UPDATED URL, see note below *)

URLENCODED_URI is the site you wish to know the number of +1's for, e.g. http://www.google.com (http%3A%2F%2Fwww.google.com)

For example, fetch the URI https://plusone.google.com/_/+1/fastbutton?url=http://www.google.com/ (UPDATED URI) and locate the first occurance of window.__SSR = {'c': 32414.0 ,'si'. Preferably use regexp for this, but I'll leave the implementation to you and your chosen programming language (server side or client side).

The float number following 'c' is the number of +1's the site have. For google.com this is 32,414. Don't worry about the float, you can safely convert it to an integer.

* UPDATE: The URL has been updated as the old URL started to 404. Remember, this is expected as this is an unofficial method. There exist no official method (yet).

Grit answered 13/9, 2011 at 21:58 Comment(7)
Apparently it is a way to go. I'll give it a try and post the results here later.Eldreda
would you mind telling me how did you figure this out, or where did you find this answer?Eldreda
I inspected the network traffic caused by the plus button on a few sites (credits goes to the lovely WebKit Inspector in Safari and Chrome) and found out that they all behaved in a certain manner, and they all called a certain url which in turn returned html and javascript. I found that the javascript contained the number of +1's, regardless of wether the number of +1's was represented as 32k, 'hundreds' or 14.Grit
This pointed me in the right direction, I now have a cookie-free asynchronous call to get the google plus count to go along with my facebook, twitter, stumble upon and delicious calls.Lamella
This seems good, I was considering a solution like this, but I am having a problem do you think you could help, this url https://mcmap.net/q/539368/-google-plus-count-urlFabrication
Use the URL plusone.google.com/_/+1/… instead and follow the same steps.Grit
@VinnyD, check my answer, which is client-side.Depreciable
R
4

Could you use a callback function to grab the value of the div that displays the count?

function count() {
    var count = $('#aggregateCount').html();
}

    <g:plusone callback="count()"></g:plusone>

I'm basing this off the bubble annotation button, I haven't tested it but something like this should work.

Rosemarierosemary answered 13/9, 2011 at 15:10 Comment(3)
As far as I understand the callback attribute, it is only called once the user clicks the button.Eldreda
ah, what about an onload then?Rosemarierosemary
i cant seem to find that event, @barro32. The available events are all user interaction related.Eldreda
D
2

A pure client-side solution that works for me to get the Google Plus counter is as follows. It does not need an API key.

var url = "http://www.yoursite-to-be-counted.com";
var data = {
    "method":"pos.plusones.get",
    "id": url,
    "params":{
        "nolog":true,
        "id": url,
        "source":"widget",
        "userId":"@viewer",
        "groupId":"@self"
    },
    "jsonrpc":"2.0",
    "key":"p",
    "apiVersion":"v1"
  };
  $.ajax({
    type: "POST",
    url: "https://clients6.google.com/rpc",
    processData: true,
    contentType: 'application/json',
    data: JSON.stringify(data),
    success: function(r){
      setCount($(".google-plus-count"), r.result.metadata.globalCounts.count);
    }
  });

  var setCount = function($item, count) {
    if (count) {
      $item.text(count);
    }
  };

Then I have some html with

<div class="google-plus-count"></div>

Credits here goes to this answer.

Depreciable answered 18/11, 2015 at 16:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.