It seems that the Twitter web intents tweet event returns a response as soon as the tweet is opened instead of returning a response when the post is actually published. Is there any way to return something once the post is actually published and return null or undefined if the window is closed without the post being published?
I’m binding the tweet event to a click event on several elements on a page like this:
$.each($('a[data-bird].twitter'), function() { //Add tweet href to Twitter buttons on page load
var $birdName = $(this).attr('data-bird'),
$birdImage = $(this).attr('data-birdimage'),
$birdText = $(this).attr('data-twitter');
$(this).attr('href', 'https://twitter.com/intent/tweet?text=' + encodeURIComponent($birdText) + '+BirdsTellUs.org+%23COP21+' + $birdImage);
});
twttr.ready(function (twttr) { //When Twitter is ready
//console.log('Twitter Ready');
$('a[data-bird].twitter').on('click', function() {//When Twitter link clicked
var $birdName = $(this).attr('data-bird'),
$socialChannel = $(this).attr('data-smchannel'),
$trackShare = function() { //Track Twitter sharing function
dataLayer.push({
'event':'SMShare',
'vpurl': $birdName,
'channel': $socialChannel
});
};
twttr.events.bind('tweet', function(intentEvent) { //Bind tweet event
if (!intentEvent) {
return;
} else {
if ($swapBirds === undefined) { //If no cookies are set
Cookies.set('shownBirds', $birdName, { path: '' }); //Set cookies
$swapBirds = Cookies.get('shownBirds'); //Update variable
$('img[data-bird="' + $birdName + '"]').attr('src', 'img/birds/' + $birdName + '.png'); //Swap image
$thankYou(); //Show thank you message
//$showEasterEgg(); //Show easter egg
$trackShare(); //Track sharing event
} else { //If cookies are set
if ($.inArray( $birdName, Cookies.get('shownBirds').split('&') ) !== -1) { //If bird is in array
return; //Do nothing if image has already been shared
} else if ($.inArray( $birdName, Cookies.get('shownBirds').split('&') ) === -1) { //If bird is not in array
Cookies.set('shownBirds', $swapBirds + '&' + $birdName, { path: '' }); //Set cookies
$swapBirds = Cookies.get('shownBirds'); //Update variable
$('img[data-bird="' + $birdName + '"]').attr('src', 'img/birds/' + $birdName + '.png'); //Swap image
}
$thankYou(); //Show thank you message
//$showEasterEgg(); //Show easter egg
//$goToNewSlide(); //Go to easter egg slide
$trackShare(); //Track sharing event
}
}
});
});
});