Getting an activity ID from a Google+ Post URL

Screenshot of demo application running

Taking the URL to a post and then determining the post ID for additional API calls is a little tricky but also fundamental for the Google+ public data API. First, you have to list activities and then search through them to determine which is the actual activity that you’re searching for. To show you how to take a URL from Google+ and determine the activity ID, I have created a demo application that finds the ID for a post given the URL to the post on Google+. Let’s walk through the demo!

This demo is very minimal, all it does is:

  1. Parse the user ID from the URL.
  2. Search the activities for the given URL.
  3. Match an activity with the same canonical URL link as the post.
  4. Show the activity ID.

The following code parses the user ID from the URL:

  function search(){
    // url is in a text input element with id 'url'
    var url = document.getElementById('url').value;
    searchForUrl(url, 0, '');

  function searchForUrl(searchUrl, queryCount, nextPageToken){
    var userId = searchUrl.split('/')[3];


Note that should the URL change, the code too must be updated to reflect the new point for the userid. However, because the API is RESTful, this structure should be pretty stable.

The following code searches for activities:


    // Set parameters for the query
    var URL        = '' + userId
      + '/activities/public?key=' + key 
      + "&orderBy=recent&pageToken="+nextPageToken;

    performXHR(URL, searchUrl, queryCount, nextPageToken);

  // performXHR - used to trigger the XMLHttpRequest
  function performXHR(URL, postUrl, nextPageToken){
    var request = new XMLHttpRequest();

    request['postUrl'] = postUrl;
    request['nextPageToken'] = nextPageToken;'GET', URL, true);
    request.send(); // because of "false" above, will block until the request is done 
                    // and status is available. Not recommended, however it works for simple cases.

    request.onreadystatechange = function(){ 
      if (request.readyState == 4) {
        var query = request['postUrl'];
        var nextPageToken = request['nextpagetoken'];

        var activities = JSON.parse(request.responseText).items;
        nextPageToken = JSON.parse(request.responseText).nextPageToken;

        for (value in activities){

        return handleActivities(activities, query, nextPageToken);

This code is a little more complicated. First, the activties/list RESTful API endpoint is formed using the userid from the previous step. Next, the XmlHttpRequest (XHR) is formed and sent.  Finally, the results are parsed into a JSON object.

The following code traverses the activities and tries to match against the url values:

  // handleActivities - Parses and stores activities from the XMLHttpRequest
  function handleActivities(activities, postUrl, queryCount, nextPageToken){
    for (var activity in activities){
      activity = activities[activity];
      if (activity['url'] == postUrl){
        targetActivity = activity;
        document.getElementById('result').value= 'ID is:n' +;
        isFound = true;

    if (queryCount < maxQueryCount && !isFound){

      // throttle calls using timer to avoid reaching query limit
      setTimeout(searchForUrl(postUrl, queryCount, nextPageToken), 100);

This code just walks through the returned activities and then checks if any of the activities references the URL being searched for.  If the URL is found, the result is displayed,. IF the URL is not yet found, additional queries are performed, passing the next page token to go deeper into the user’s activities.

The following code is the relevant part from the previous step that shows the id:

      if (activity['url'] == postUrl){
        targetActivity = activity;
        document.getElementById('result').value= 'ID is:n' +;
        isFound = true;

Please feel free to ask any questions you have in the comments or on the Google+ link used to +1 / share the post.