Custom social sharing buttons Part 3 – Pinterest

(Note – if you haven’t already, at the very least I suggest you read the first post in this series on custom social sharing buttons for Facebook, as it contains information that is necessary to the overall picture, but which isn’t being repeated in this post. The second post in this series dealt with Twitter).

As with the first two posts in this series, the aim of the game is two parts – firstly to get the relevant data from Pinterest’s API, and secondly to update our custom social sharing button with the data returned.

As always, the information is obtained through a JSON request to Pinterest’s API – using the first line of the code (below), the count is returned in the following format :

receiveCount({"count": 0, "url": "https://www.hummingbirduk.com/custom-social-sharing-buttons-part-1-facebook"})

From that, it’s a simple enough step to, as with the Twitter count before it, check and see that (a) the count is not 0, (b) is not null and (c) is not “-” (one of the potential values returned). All three of those being the case, we then use the count to update our custom Pinterest button.

$.getJSON("http://api.pinterest.com/v1/urls/count.json?url=<!--?php echo $canonURL ?-->&amp;callback=?", function(data) {
	if ((data.count != 0) &amp;&amp; (data.count != undefined) &amp;&amp; (data.count != null) &amp;&amp; (data.count != "-")) {
		$('#pinterestLink').removeClass('sharing-button-has-no-count');			
		$('#pinterestLink').addClass('sharing-button-has-count');	
		$('#pinterestButton').append(beforecounter + data.count + aftercounter);	
	}				
});

The actual action part of the button, however, is a little more complicated than Twitter and Facebook, because Pinterest wants the description, the URL of the page to be pinned, and the URL of the image to be pinned, to be passed to it as parameters in the URL of the window being opened – it may well therefore take a little bit of trial and error to get this side of things working properly. The Google Event Tracking and window open code looks like this :

function pinterest_popup() {
	_gaq.push(['_trackEvent', 'Social Media (product page)', 'Pinterest', 'Pin',, false]);
	window.open( "http://pinterest.com/pin/create/button/?urlPAGE_URL&amp;media=MEDIA+URL&amp;description=DESCRIPTION", "pinterestWindow", "height=270,width=630,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0" )
}

And there you have it.

3 comments

  • Interested

    Will you be doing the Google+ count soon?

    • Giles (author)

      Unfortunately, Google+ doesn’t have an API, so it’s quite difficult to get the count. I’ll do a post once I figure out a method of getting the information!

      Giles

    • Giles (author)

      As promised, I’ve done a little post on this subject, which you can find here.

      Giles

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to talk to us about your project?