Social‎ > ‎Technotes‎ > ‎

GApps Sites, GAS Tracking within your Domain

posted Jan 28, 2013, 8:14 AM by Faustino Rodriguez   [ updated Jan 28, 2013, 8:43 AM ]
To Track Or Not To Track? 
Tracking visitors in your Web site is great for statistics but I would say, do it always Anonymously, in other words, do not collect neither track personal behavior, it's not polite nor completely legal. Particularly if you do it using GA (Google Analytics, see TOS #7 Privacy).

However, there are some circumstances when monitoring user's visits maybe be right and even legal, like when you have a private Google Site just for the users within your domain and you have full disclosure of this tracking and a privacy policy to support it. It's like having your own Intranet in the Cloud. In any case, consult your legal adviser to be safe.

Now, let's deal with the technicalities around monitoring user's visitors within your domain site using Google Apps Script, step by step simplified:
  • Have a Google Site just shared within your domain, so users have to be logged in with an account in your domain to have access to this website.
  • Have a Google Analytics Property ID that you want to associate just with this site monitoring, like UA-12345678-1
  • Within this Site go More > Manage site > Apps scripts > Add new script, with the following content
function doGet() {
  var utmUrl = gaTrack("UA-12345678-1");
  return HtmlService.createHtmlOutput("Privacy Note: This page is monitored for statistics and internal use.");
}
function gaTrack(propertyId, path) {
  try {
    var utmGifLocation = "http://www.google-analytics.com/__utm.gif";
    var path = SitesApp.getActivePage().getName();
    var utmUrl = utmGifLocation + "?" +
        "utmwv=4.4sa" +
        "&utmn=" +  (new Date().getTime())+ //unique - cache buster
        "&utmhn=" + Session.getUser().getEmail().split("@")[0] +
        "&utmp=" + encodeURIComponent(path) + 
        "&utmac=" + propertyId +
        "&utmcc=__utma%3D999.999.999.999.999.1%3B" +
        "&utmvid=" + Math.random() +
        "&utmip=" + "-"; // "&utmip=" + "-"
    var fetch = UrlFetchApp.fetch(utmUrl);
    return utmUrl;
  } catch (e) {
    return "Error on utmUrl" + utmUrl;
  }
}
  • Save the script and name it gaTracking (or any other name)
  • Follow this sequence to publish the script: File > Manage version > write some description and save new version, then Publish > Deploy as web app... > Select the project version > Execute as user accessing the web app > Access anyone within your domain > Deploy
  • Go back to your website and select a edit the first page you want to monitor > at the end of that page > Insert > Apps Script Gadget > Select (✔) the script you just published > Select > Uncheck border and title > Set the Height > Save
  • About the Gadget Height: at least you would need a main page with that gadget height about 500 pixel to allow users on their first visit to authorize the script to run the Sites service
  • Then insert the same Apps Script Gadget to any other pages you want to monitor
  • To check how your users are visiting those pages go to Google Analytics and select the Property ID you have assigned to the script and look under > Reporting > Content > Site Content > All Pages > Sort by Page > Add a Secondary dimension > Content > Hostname >> and you would see your users, site pages and pageviews there
I believe, that's it
Share your comments and question with g+1 below, I will thank you for that ;)

Comments