Is there a callback for Twitter’s Tweet Button?

Twitter has Web Intent events for loaded, rendered, resize, tweet, follow, retweet, like, and click.

twttr.events.bind(
  'tweet',
  function (event) {
    // Do something there
  }
);

The behavior was changed in the fall of 2015 due to the unreliableness of callbacks happening after an event is complete.

They will now be triggered when a user invokes the action in your page, rather than after the action is completed.

Example loading widgets.js:

<script>
// Performant asynchronous method of loading widgets.js
window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return t;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);

  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };

  return t;
}(document, "script", "twitter-wjs"));
</script>

<script>
// Wait until twttr to be ready before adding event listeners
twttr.ready(function (twttr) {
  twttr.events.bind('tweet', function(event) {
    console.log(event);
  });
});
</script>

Leave a Comment