Twitter Javascript with URL redirect?
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

The code below allows a user to follow a Twitter account from a web interface.

I'm trying to figure out how to modify the code below to redirect a user to a given URL (www.example.com) after a user who is not currently following starts following. Can you help?

<a href="https://twitter.com/USERNAME" class="twitter-follow-button" data-show-count="false" data-size="large" data-dnt="true">Follow @USERNAME</a><script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
Where did you get this snippet from?
sguha almost 9 years ago
From here: https://twitter.com/about/resources/buttons
Jay almost 9 years ago
Their code basically creates an iframe on the page so it is hard and complicated to even run javascript code to look and see if they are a fan based on the HTML twitter shows.
skram almost 9 years ago
Thanks for the feedback, is there any other way of doing this? Perhaps redirecting user to a given URL after they clicking on the button, rather than verifying whether or not they're a user?
Jay almost 9 years ago
The 'cleanest' way to do this would be to require OAuth which, IMHO, would likely decrease the overall amount of followers you get due to the burden of onboarding them as a follower.
skram almost 9 years ago
If this is super important to you, my suggestion would be show an image like the follow button and when the user hovers over it, you store analytics that they were interested in and/or clicked the button..... You cannot count the # of clicks on the iframe, but if you are OK with showing a button (non-Twitter official one via their JS) and counting clicks on that, you could do so with your web analytics software like Google Analytics or Omniture.
skram almost 9 years ago
Skram, actually, it may be easier to explain the end goal, and there may be a more elegant solution. Am trying to set up conversions within Adwords: https://support.google.com/adwords/answer/1722054 I'm relying on another page loading to track it - can it be done while still retaining a single-click follow for users?
Jay almost 9 years ago
To my knowledge, you cannot do this with regards to the usual Twitter iframe follow button because the tracking javascript (you cant change the HTML they give you) would be a cross domain injection. What you need to do is not use the Twitter-specified javascript button and create your own with an onclick event, as mentioned on the page you linked to and http://stackoverflow.com/a/5092940
skram almost 9 years ago
awarded to alv-c

Crowdsource coding tasks.

2 Solutions


The code at http://jsbin.com/uxanok/22/edit should work, but as @Jay and I have been communicating over email, it doesn't seem to be.

Winning solution

You can use twitter events to do that.

Some example code:

twttr.events.bind("follow", function(){ console.log('New followah!'); });

Put that after the inclusion of twitter's widgets.js file.

Your final code should look like:

twttr.events.bind("follow", function(){ window.location = 'www.example.com'; });

View Timeline