Passive Sharing: Writing app activities without target URLs

It is a best practice to target your shares to target pages containing Schema content but sometimes you cannot do this. For example, if you are referencing content that is not on your site or if you are writing app activities from mobile devices or Google Chrome extensions, you will need to feed the “schemified” data directly to Google. In this post, I cover how this is done using JavaScript but there are more examples on the Google+ developers site that cover writing app activities using Android without a target URL and writing app activities in other programming languages.

Demo

The following page demos writing app activities without target URIs. Writing app activities like this is appropriate in various cases such as mobile-only scenarios and chrome extensions:

Google+ Platform Demo: Writing moments without target URIs

The key pattern you will notice is as follows:

    writeMoment: function(){
      var payload = {
        "target": {
          "id" : "replacewithuniqueidforaddtarget",
          "image" : "http://www.google.com/s2/static/images/GoogleyEyes.png",
          "type" : "http://schema.org/CreativeWork",
          "description" : "The description for the activity",
          "name":"An example of AddActivity"
        },
        "type":"http://schemas.google.com/AddActivity",
        "startDate": "2012-10-31T23:59:59.999Z"
      };
      var args = {
        'path': '/plus/v1/people/me/moments/vault',
        'method': 'POST',
        'body': JSON.stringify(payload),
        'callback': function(response) {
           console.log(response);
         }
      };

      gapi.client.request(args);

What you will notice is the payload ‘target’ field is JSONified schema markup. Conventionally, this is determined by microdata on the page, but again, sometimes you want to write app activities that don’t have corresponding web pages – the best examples I can think of are when your app is a Chrome extension or Mobile-only. The following additional examples highlight a few different types of activities that you might want to write.

Add Activity

    writeAddActivity: function(url){
      var payload = {
        "type":"http://schemas.google.com/AddActivity",
        "startDate": "2012-10-31T23:59:59.999Z"
      };
      if (url != undefined){
        payload.target = {
          'url' : 'https://developers.google.com/+/plugins/snippet/examples/thing'
        };
      }else{
        payload.target = {
          "id" : "replacewithuniqueidforaddtarget",
          "image" : "http://www.google.com/s2/static/images/GoogleyEyes.png",
          "type" : "http://schema.org/CreativeWork",
          "description" : "The description for the activity",
          "name":"An example of AddActivity"
        };
      }
      this.writeAppActivity(payload);
    },
    writeAppActivity: function(payload){

      gapi.client.plus.moments.insert(
          {  'userId' : 'me',
             'collection' : 'vault',
             'resource' : payload
          }).execute(function(result){
              console.log(result);
          });
    }

Listen Activity

    writeListenActivity: function(url){
      var payload = {
        "type": "http://schemas.google.com/ListenActivity",
      }

      if (url != undefined){
        payload.target = { 'url' : url };
      }else{
        payload.target = {
          "type": "http://schema.org/MusicRecording",
          "id": "uniqueidformusictarget",
          "description": "A song about missing one's family members fighting in the American Civil War",
          "image": "https://developers.google.com/+/plugins/snippet/examples/song.png",
          "name": "When Johnny Comes Marching Home"
        };
      }

      this.writeAppActivity(payload);

Final notes

When you are writing app activities, make sure that you target the BEST app activity type. What this usually means is avoiding the Add activity if possible. If you can change the target schema for your activities to include more relevant information, do it.  If you can change the way that your app works to target a new type of activity, do it! Again, I want to reiterate that unless you have to, write your app activities to Google using Schema instead of directly pumping Schemified JSON to Google.

Watch the Google+ sessions at IO to learn about sharing and the latest features from the Google+ team.