Skip to content

Instantly share code, notes, and snippets.

@ccschmitz
Last active September 18, 2017 06:55
Show Gist options
  • Save ccschmitz/7810392 to your computer and use it in GitHub Desktop.
Save ccschmitz/7810392 to your computer and use it in GitHub Desktop.
Display content only after a user shares something via Twitter, Facebook or Google+
<div id="fb-root"></div>
<script>
function showDiscountCode(response) {
// if they unliked you...
if (response && response.state === 'off') {
hideDiscountCode();
} else {
$('.discount-code').slideDown('slow');
}
}
function hideDiscountCode() {
$('.discount-code').slideUp('slow');
}
// load js for facebook
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
// load js for g+
(function() {
var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
po.src = 'https://apis.google.com/js/platform.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
})();
// load js for twitter
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"));
// sets up event listeners for when someone (un)likes the page
window.fbAsyncInit = function() {
FB.init({ appId: 'YOUR_APP_ID', status : true, cookie : true, xfbml : true });
FB.Event.subscribe('edge.create', function(response) { showDiscountCode(); });
FB.Event.subscribe('edge.remove', function(response) { hideDiscountCode(); });
};
// setup event listener for tweet callback
twttr.ready(function (twttr) {
twttr.events.bind('tweet', function(event) {
showDiscountCode();
});
});
</script>
<!-- the sharing buttons -->
<a href="https://twitter.com/share" data-text="Your tweet text" data-url="http://your-url-here.com" data-via="YourHandle" class="twitter-share-button" data-lang="en"></a><br>
<div class="fb-like" data-href="http://www.facebook.com/your_fb_page" data-layout="button_count" data-action="like" data-show-faces="false" data-share="false"></div><br>
<div class="g-plusone" data-size="medium" data-annotation="inline" data-width="300" data-callback="showDiscountCode" data-href="http://your-url-here.com"></div><br>
<!-- the hidden content -->
<div class="discount-code">The discount code is: <strong>abcd1234</strong></div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment