While adding Tweet Buttons to a website I am working on I decided instead of reinventing the wheel I would just use the simple code provided by Twitter here: http://dev.twitter.com/pages/tweet_button. Even the most "complex" example on the page is pretty simple, such that anyone can implement it. Which is great, I applaud Twitter for that. However, there are a couple of gotcha's that I suspect other people might run into.
The widget.js you include from Twitter's site will work it's magic, and one of the steps in making the magic is sending a request to http://urls.api.twitter.com/1/urls/count.json?url=yourencodedurl&twitter.callback=somefunction to get the count of how many times this URL has been shared. The problem with this is that the JSON from this is cached by your browser. A hard refresh won't clear the result. Closing your browser won't clear the result. I didn't try deleting all my cookies or clearing my browsing data, that might actually work, however, that's obviously less than ideal just to see if the number has incremented or not. I found this out because in Firefox the button read 393, and Chrome read 421 a few minutes later, and I couldn't get either number to change.
Since this is pointless to me, if people only get to see whatever the number was when they first visit the page, and I couldn't figure out how to tack on a URL variable to that URL in Twitter's minified widget.js, I decided to do it myself. Pardon my function names, this was a bit frustrating for me, let me reiterate, I like Twitter. Anyway the HTML looks like this:and the following JS makes the button:
This basically makes a jsonp request to the same URL that Twitter does, and parses out the count, but has the added bonus of not being cached by the browser. To be safe, I'll probably change twitterCacheSux=1 to some GUID, but the basic principle remains the same.
If you want a little twitter styling to the button you can add the following CSS:
Tweets by an account without any followers will not be counted in the Tweet counter. I had done a lot of this with a test account I made as to not spam any real followers. However, whenever I used the test account the counter never incremented. When I'd test with my real account, the counter incremented. So I added a follower to the test account and sure enough that account started being counted as well. /annoying
Hopefully that helps someone else from spending time figuring out why the increment isn't working for them in the Tweet Button.