My goals are, on click:
Share URL and text on Twitter in popup window
Receive response from Twitter when tweet is successful or when tweet fails
Close the popup window after tweet is successful or when user closes the window
My problems are:
I do not receive response from Twitter when tweet is successful or when tweet fails
Popup window remains open after tweet is successful
I have tried a lot of options on SO so far
This code is in the HTML body tag
<script>
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>
Function to pen in popup
function share_to_twitter(sParamUrl, sParamText, iParamWinWidth, iParamWinHeight) {
var iWinTop = (screen.height / 2) - (iParamWinHeight / 2);
var iWinLeft = (screen.width / 2) - (iParamWinWidth / 2);
window.open('http://twitter.com/intent/tweet?url='+encodeURIComponent(sParamUrl) + '&text=' + sParamText,'top=' + iWinTop + ',left=' + iWinLeft + ',toolbar=0,status=0,width=' + iParamWinWidth + ',height=' + iParamWinHeight, '_blank');
}
share_to_twitter('https://example.com','Text to tweet' 640, 480);
Receive response from Twitter and take action
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function(event) {
alert(event);
/*Example:
if(event == 'success'){
window.close();
}
else{
//Do something else
}*/
});
});
I got a response when I opened the Twitter link directly like so instead of in a popup:
<a href = "http://twitter.com/share?text=text goes here&url=http://example.com" target = "_blank">Click here to tweet</a>
However, according to https://twittercommunity.com/t/callback-function-after-a-successfull-tweet/65409/2 a Twitter staff replied as follows
Unfortunately, no. We don’t provide an event when the tweet was actually posted, only an event when there was a click to open the popup. The code you already have is probably the best guess for analytics.
However, another Twitter staff suggests using search or timeline API to check whether the message was posted. > https://twittercommunity.com/t/callback-function-after-a-successfull-tweet/65409/4
You’d have to know the username, and then use the search or timeline API to check whether the message was posted.
You cannot know from the OP's code whether the tweet was posted successfully. However, here is how to get response regarding Twitter tweet,
Put the following code in the HTML body tag after the <body>
tag:
<script>
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>
Then, here is the link for a visitor to click to tweet; customize for your needs:
<a href = "http://twitter.com/share?text=text goes here&url=http://example.com" target = "_blank">Click here to tweet</a>
And here is the code to receive response from Twitter
twttr.ready(function (twttr) {
twttr.events.bind(
'tweet',
twitter_share_callback);
});
function twitter_share_callback(response) {
console.log(response);
}
Note: Response from the code above is received the moment the visitor or user opens the link for tweeting. You receive the response whether or not the tweet was posted. and it doesn't help you to know whether the tweet was successful or not