google+ share and onendinteraction - no confirm

夙愿已清 提交于 2019-12-07 06:22:49

问题


I'm setting up a google+ share button, and want to know when someone has shared the link so that I can perform an action. You can register a callback on the share button using the onendinteraction attribute, and the documentation states that this will be called whenever the share box is closed and whenever the share is completed.

My function is being called when the window is closed, but not when the link is actually shared:

function redirectGooglePlus(jsonParam) {
    alert(jsonParam.type);
}
<div class="g-plus" data-action="share" data-annotation="vertical-bubble" data-height="60"
                 data-href="http://mywebsite.com" data-onendinteraction="redirectGooglePlus"></div>

My function redirectGooglePlus is only ever called when the hover type, and never confirm (which is the one that is supposed to signify the share has been completed.

Does anyone know why the function isn't called with confirm?

FYI the google share documentation is here: https://developers.google.com/+/web/share/


回答1:


So this appears to be a bug with the current implementation of the google+ share button:

https://code.google.com/p/google-plus-platform/issues/detail?id=396

The (hideous) workaround I've used for now is to look for 2 hover events for the onendinteraction. If the events come in quick succession (less than 1 second), then it's likely they've shared the item.




回答2:


Quite late on this one so it might not be relevant but according to Google Developer page for their web platform, it seems you can tap into the current users activities list using a javascript code similar to -

var request = gapi.client.plus.activities.list({
  'userId' : 'me',
  'collection' : 'public'
});

request.execute(function(resp) {
  var numItems = resp.items.length;
  for (var i = 0; i < numItems; i++) {
    console.log('ID: ' + resp.items[i].id + ' Content: ' +
      resp.items[i].object.content);
  }
});

The online tool to generate and test the query end point is there on the Developer Page

Generate and append a custom query string to the end of the link your user is sharing, the JSON returned from the end point can be parsed to check if that particular link has been shared on the users activity stream. The JSON returned looks like this -

{
 "items": [
  {
   "title": "",
   "published": "2015-06-12T16:39:11.176Z",
   "url": "https://plus.google.com/+UserID/posts/PostID",
   "object": {
    "content": "",
    "attachments": [
     {
      "objectType": "article",
      "url": "http://www.example.com"
     }
    ]
   }
  }
 ]
}

If the link with the custom query is there in the attachments section of one of the returned items,Voila! Its been shared.




回答3:


A Possible solution to this would be the following as a work around.

  1. Make the users open a new window which contains ONLY the Share Button Code passing the url to be shared
  2. Set a variable to 0 on window load
  3. In your function redirectGooglePlus update the variable to 1 when data-onendinteraction is called
  4. Check the window for close and verify the variable was set

    function closeWin(){
    
     if(x==0){
        //not shared before leaving code;
     }else{
        //shared before window closed;
     }
    }    
    body onbeforeunload="closeWin()" 
    


来源:https://stackoverflow.com/questions/18375727/google-share-and-onendinteraction-no-confirm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!