Selectively rendering interactive posts

In some rare cases, it makes sense to render interactive posts selectively, falling back to the share button.  For example, you might want to do this when your uses could be fickel enough to drop off when prompted to sign in but still might share content without being signed in to Google+ on your site. In most cases you should not be doing this because the user will automatically be prompted to sign in to your site if they haven’t already and the interactive post button is more engaging than the share button.

Anyways, if you want to do this (and again, this is in extremely rare cases) the following code will get you started.

In short, the page will render an interactive post as follows when logged in to the site:

When not logged in, the page will render a share dialog:

The following HTML shows markup where both the interactive post and share buttons are rendered within hidden div tags:

  <title>Google+ JavaScript Quickstart</title>
  <script type="text/javascript">
  (function() {
    var po = document.createElement('script');
    po.type = 'text/javascript'; po.async = true;
    po.src = '';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(po, s);
  <!-- JavaScript specific to this application that is not related to API
     calls -->
  <script src="//" ></script>
  <!-- Place the tag where you want the button to render -->
  <div id="ipost" style="display:none">
      data-prefilltext="Engage your users today, create a Google+ page for your business."
    <button>Tell your friends</button>
    <button id="disconnect">Disconnect</button>
  <div id="share" style="display:none">
    <g:plus action="share"></g:plus>
    <span id="signinButton">
      <u>Click to upgrade</u>

The following javascript sets up the disconnect button and adds handlers for the signin callback for the interactive post button.


$(document).ready(function() {

function onInteractiveCallback(authResult){
  if (authResult['error']) {
    // There was an error, which means the user is not signed in.
    // As an example, you can handle by writing to the console:
    console.log('There was an error: ' + authResult['error']);
    $('#authResult').append('Logged out');
  } else {

function disconnect(){
 // Revoke the access token.
   type: 'GET',
   url: '' +
   async: false,
   contentType: 'application/json',
   dataType: 'jsonp',
   success: function(result) {
     console.log('revoke response: ' + result);
   error: function(e) {

When the sign in callback fails when it is triggered by immediate mode indicating the user isn’t signed in, the div showing the share button is rendered along with the Google+ sign-in code. If the user signs in, they get the interactive post button, if they disconnect, they will see the share button.

Demo project here, sources on GitHub here.