Thursday, May 10, 2012

Google Plus One button callback - Any way to "subscribe" to the +1 action?


I'm looking for a way to subscribe to the plus one button.



According to the documentation here: https://developers.google.com/+/plugins/+1button/#plusonetag-parameters I could add a callback attribute to the tag, but in my case I'm not allowed to interfere. I'm building a tool ontop of the site, an embedded JS triggered on document ready. I want to add the callback live, and it mustn't interfere the original callback if one was declared.



I don't have this problem with Facebook or Twitter (like and tweet, for instance) . In these cases there's the FB and twttr global variables, registered like so once they are available:




FB.Event.subscribe("edge.create", function(e) {
console.log(e);
})



or twitter's twttr.events.bind ...



Am I missing anything or is Google choosing a very awkward way to do things? What's their interest in this method and what can be done around it?


Source: Tips4all

2 comments:

  1. You can use the JavaScript API to retrieve the +1 callback.

    gapi.plusone.render(
    myDomNode,
    { "callback": myCallbackFunction });


    Or you alternatively can specify the "callback" attribute if you're using the DOM version.

    In either case, the callback will be invoked with an object which has two properties: href returns the URL which was +1'd, and state is either "off" or "on".

    ReplyDelete
  2. You could go one further than Daves answer, and indeed inject your own callback - but take the extra step of retrieving the existing callback value beforehand and dispatching it yourself within your own handler (if there is an existing callback value) with the same values as your callback received.

    That way both your handler and the original handler will be called, and hopefully no one would be any the wiser :)

    ReplyDelete