Profile View Counter

From OpenSocial Directory

Jump to: navigation, search
Thumbnail
counter.png
Application Details
Description Profile view counter (uses persistent data)
Publisher James Peter
Author James Peter
Code http://open-counter.googlecode.com/files/OpenSocial_Profile_Counter.xml
Genre Tools
Supports OpenSocial

Image:Example.jpg==Reviews== Add a Review
Template:Profile View Counter Reviews

[edit] Comments

Leave a comment

Your Name:
Heading:
Comment:



comment posted by: bhaskar bhamidi (Fri Oct 03 17:45:52 2008)


[edit] Code

View Code

<?xml version="1.0" encoding="UTF-8" ?> 
<Module>
  <ModulePrefs
    title="Counter"
    directory_title="Counter"
    description="Counter records who visits your profile! On your profile it displays the number of people who have seen your profile along with the details of the last person who visited."
    author="James Peter"
    author_email="zemaj.com+countergadget@gmail.com"
    thumbnail="http://open-counter.googlecode.com/files/counter.png"
    height="50">
  <Require feature="opensocial-0.5"/>
  <Require feature="dynamic-height"/>
  <Require feature="analytics"/>
 </ModulePrefs>
 <UserPref name="showOwner"
    display_name="Include your own views in the counter?"
    datatype="bool"
    default_value="true" />
 <Content type="html"><![CDATA[

  <script type="text/javascript">
    // NB: We shouldn't have to hard code this!
    // There seems to be no way to get this data & the profile urls are relative :(
    var _urlBase = "http://sandbox.orkut.com";
    var _prefs = new _IG_Prefs();

    function loadCounter() {
      var req = opensocial.newDataRequest();
      req.add(req.newFetchPersonRequest('VIEWER'), 'viewer');
      req.add(req.newFetchPersonRequest('OWNER'), 'owner');
      req.add(req.newFetchInstanceAppDataRequest("*"), "instanceData");
      req.send(showCounter);
    }

    function getInstanceData(data) {
      var data = data.get("instanceData");
      if(data && !data.hadError()) {
        return data.getData();
      }
      return null;
    }
    
    function getInstanceKey(instanceData, key) {
      if(instanceData)
        return instanceData[key];
      return null;
    }

    function showCounter(data) {
      if(data.hadError()) { console.log(data.getError()); }
      var owner = data.get("owner");
      var profile_name = "This";
      if(!owner.hadError()) {
        profile_name = owner.getData().getDisplayName();
        if(profile_name.charAt(profile_name.length-1) == "s") {
          profile_name = profile_name+"'";
        }
        else {
          profile_name = profile_name+"'s";
        }
      }

      var instanceData = getInstanceData(data);
      var counter = parseInt(getInstanceKey(instanceData, "counter"));
      if(!counter) {
        counter = 0;
      }

      var viewer = data.get("viewer");
      var viewData = null;
      if(!viewer.hadError()) {
        viewData = viewer.getData();
        if(_prefs.getBool("showOwner") || !viewData.isOwner()) {
          ++counter;
        }
      }
      document.getElementById('counter_container').innerHTML = profile_name+" profile has been viewed "+counter+" times.";

      var lastVisitor = getInstanceKey(instanceData, "lastVisitor");
      var lastVisitorUrl = getInstanceKey(instanceData, "lastVisitorUrl");
      if(lastVisitor) {
        if(lastVisitorUrl) {
          lastVisitor = "<a target='_top' href='"+_urlBase+lastVisitorUrl+"'>"+lastVisitor+"</a>";
        }
        document.getElementById('lastVisitor_container').innerHTML = "The last person to visit this profile was "+lastVisitor+".";
      }
      else {
        lastVisitor = "";
        lastVisitorUrl = "";
      }

      var req = opensocial.newDataRequest();
      req.add(req.newUpdateInstanceAppDataRequest("counter", ""+counter)); // Can't pass primatives
      
      var viewer = data.get("viewer");
      if(viewData) {
        if(_prefs.getBool("showOwner") || !viewData.isOwner()) {
          lastVisitor = viewData.getDisplayName();
          lastVisitorUrl = viewData.getField(opensocial.Person.Field.PROFILE_URL);
          req.add(req.newUpdateInstanceAppDataRequest("lastVisitor", lastVisitor));
          req.add(req.newUpdateInstanceAppDataRequest("lastVisitorUrl", lastVisitorUrl));
        }
      }

      req.send(savedCounter);
      _IG_AdjustIFrameHeight();
    }

    function savedCounter(data) {
      if (data.hadError()) {
        console.log(data.getError());
      }
    }

    _IG_RegisterOnloadHandler(loadCounter);
  </script>
  <script>
    _IG_Analytics("UA-1239903-2", "/counter");
  </script>
  <div id="counter_container"></div>
  <div id="lastVisitor_container"></div>

  ]]></Content>
</Module>

Page Rating

Rating: 3.2/5 (6 votes cast)

Personal tools
communication