Web Niraj
  • Facebook
  • Flickr
  • Github
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
Search the site...
  • Home
  • Blog
  • Twitter API: Tweet Button Callbacks

Twitter API: Tweet Button Callbacks

5

Recently, I’ve been creating an app that rewards users for tweeting messages. However, I wanted to avoid using OAuth authentication and full integration of the Twitter API but still be able to reward users for tweeting. Luckily, I found a way of doing so using the Tweet button and something called Web Intents.

The Twitter JavaScript API needs to be used (which is simple to integrate by copy-and-pasting the plugin code into your page). The functionality works as follows:

  1. User presses tweet button
  2. User logs into twitter (in popup) if they are not already logged in
  3. User previews message and confirms the tweet (in popup)
  4. Tweet is posted and popup closes
  5. JavaScript callback is triggered

The Tweet button handles steps 2-5 above so the only points of integration required are adding the tweet button and the callback implementation.

I integrated the Tweet button as follows:

Then, I added some extra JavaScript to the page to listen for the callback and trigger the function to reward the user:

Once a tweet has been posted, twitter will trigger the tweet event. The web intent we created will listen for the tweet event and then trigger the reward_user function.

Simples!

UPDATE (28th April 2014):

The code has been updated after Ryan spotted that the previous code was no longer working. You can see the working version of the new code on JSFiddle.

JavaScript, Twitter, Twitter API

5 comments on “Twitter API: Tweet Button Callbacks”

  1. Ryan says:
    April 28, 2014 at 4:04 PM

    Does this still work? I cannot get it to work. 50 SO points to you if you can put a fiddle together. 😉 Thanks. http://stackoverflow.com/q/23278951/834525?stw=2

    Reply
  2. Scott says:
    September 30, 2014 at 6:18 AM

    Hey Ryan,

    I’ve copied your code verbatim, but I get the following error: “Uncaught TypeError: Cannot read property ‘ready’ of undefined ” on page load.

    Reply
    • Niraj Shah says:
      September 30, 2014 at 12:05 PM

      It could be that the code for twttr.ready is being executed before twitter has a change to initialise. Have a look at the JSFiddle example and see how the code should be arranged. If the problem persists, try adding a timeout or use the jQuery ready() function to delay the event binding.

      Reply
  3. Justin says:
    January 29, 2016 at 4:01 PM

    This callback is evoked when the user clicks the share-button, not when he actually submits the tweet. The user can choose to close the dialog. I am also working on something with reward points but I need to be sure the user actually submitted the tweet.

    Reply
    • Niraj Shah says:
      February 1, 2016 at 11:14 AM

      Twitter has changed the behaviour of the click event. Previously (when this article was written), the callback was only triggered when the user successfully tweeted and so correctly tracked actual tweets. Now, it will only track the click to the button, but not if the tweet was actually posted. You will have to rely on other verification methods now, e.g. checking the user’s twitter profile programatically to see if they tweeted a specific message.

      Reply

Leave a Reply to Niraj ShahCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Firewall Flash Gadget Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins
© 2011-2025 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Privacy Policy