我想确认用户在点击Twitter Web Intent后发推文了。我该怎么做?
示例:
<a href="https://twitter.com/intent/tweet?text=Simple+Web+Intent">Tweet</a>
假设一个匿名用户点击了这个链接并发推,我如何确认这一点?最好能够得到推文的链接。
* anonymous
指未在我的网站上进行身份验证且不知道其 Twitter 用户名的用户。
我想确认用户在点击Twitter Web Intent后发推文了。我该怎么做?
示例:
<a href="https://twitter.com/intent/tweet?text=Simple+Web+Intent">Tweet</a>
* anonymous
指未在我的网站上进行身份验证且不知道其 Twitter 用户名的用户。
更新
由于Twitter更新了小部件的行为,此解决方案不再有效。现在您只能跟踪是否点击了推文按钮,而不能跟踪推文是否实际发布。
您可以使用Twitter API的“twttr.events.bind”事件监听器跟踪推文。可以在此处找到跟踪推文的工作示例:http://jsfiddle.net/EZ4wu/3/
HTML:
<a href="https://twitter.com/intent/tweet?url=https://www.webniraj.com/2012/09/11/twitter-api-tweet-button-callbacks/&text=Twitter+API:+Tweet+Button+Callbacks"><i class="icon-twitter-sign icon-white"></i> Tweet</a>
JavaScript:
function reward_user( event ) {
if ( event ) {
alert( 'Tweeted' );
console.log( event );
}
}
window.twttr = (function (d,s,id) {
var t, js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return; js=d.createElement(s); js.id=id;
js.src="//platform.twitter.com/widgets.js"; fjs.parentNode.insertBefore(js, fjs);
return window.twttr || (t = { _e: [], ready: function(f){ t._e.push(f) } });
}(document, "script", "twitter-wjs"));
twttr.ready(function (twttr) {
twttr.events.bind('tweet', reward_user);
});
我的以前的解决方案现在已经过时,似乎不再起作用了。
console.log( event.target.href );
来查看推文的内容。 - Niraj Shah$(document).ready()
中以解决问题。否则,您必须等待JavaScript加载完成后再尝试绑定。 - Niraj Shah@Niroj的答案已经不再适用了。他们更新了API,所以事件监听器会在用户点击按钮后立即触发 - 你无法检查他是否真的发推文了。
如果你自己创建窗口(而不使用Twitter小部件),你至少可以检测窗口是否关闭(无论是用户关闭还是发推文)。
不要链接小部件API!
HTML:
<a id="twitter-intent" href="https://twitter.com/intent/tweet?url=https://www.webniraj.com/2012/09/11/twitter-api-tweet-button-callbacks/&text=Twitter+API:+Tweet+Button+Callbacks"><i class="icon-twitter-sign icon-white"></i> Tweet</a>
JS:
document.getElementById('twitter-intent').addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation(); //this should do in case you don't want to unlink twitter widgets api
var width = 575,
height = 400,
left = (screen.width - width) / 2,
top = (screen.height - height) / 2,
url = this.href,
opts = 'status=1' +
',width=' + width +
',height=' + height +
',top=' + top +
',left=' + left;
var win = window.open(url, 'twitter_share', opts);
var timer = setInterval(checkWin, 500);
function checkWin() {
if (win.closed) {
//just assume the user tweeted (he could just close the window without tweeting)
alert("Thank you for tweeting!");
clearInterval(timer);
}
}
});
试试这个:
twttr.events.bind('loaded', function(event) {
window.location = "http://theredirect_link.com"
});