Wednesday, June 18, 2008

SAP Portal and Google Analytics

Some folks over at Spyvee created a document on how to integrate Google Analytics with SAP Portal. It's a very good document but, I didn't care for the fact that the code would not be entered in a standard place for javascript.

Essentially SAP takes a lot of ownership over how objects are inserted into the portal. Ideally you'd want to place the Google Anayltics code right above the tag in your page. Portal doesn't quite let you do that. At least not with any ease.

The next best place for javascript code is at the bottom of the head....at least in SAP Portal. Why? Because you can easily place it there using an AbstractPortalComponent.

Here's some modified steps to Spyvee's document that will allow you to insert Google Analytics into your Portal Framework and track a whole lot of clicks.

Netweaver Portal Integration:

When you get to this part, create a PAR with an AbstractPortalComponent. Create something like this:

package com.corp.portal.tools;

import com.sapportals.portal.prt.component.*;
import com.sapportals.portal.prt.resource.IResource;

public class GoogleAnalytics extends AbstractPortalComponent {

public void doContent(IPortalComponentRequest request, IPortalComponentResponse response) {
IResource googleAnalyticsDataResource = request.getResource(IResource.SCRIPT, "scripts/ga-split-1.js");
response.include(request, googleAnalyticsDataResource);
IResource googleAnalyticsDataResource2 = request.getResource(IResource.SCRIPT, "scripts/ga-split-2.js");
response.include(request, googleAnalyticsDataResource2);
response.setContentType(PortalComponentContentType.HTML);
}
}


What this code will do is pull two scripts that you will create in the scripts directory of the portal application. These two scripts will be the two parts of the ga.js code you grabbed from Google. The code is split into two pieces surrounded by script tags. So creating the following script files will do the trick:

ga-split-1.js:

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));


ga-split-2.js:
var pageTracker = _gat._getTracker("UA-XXXXXX-3");
pageTracker._initData();
pageTracker._trackPageview();


Obviously, don't copy this straight as you'll want your personalized tracking code instead of XXXXXXX :)

Once you've uploaded and created an iView, stash the iView at the bottom of your framework. I'm assuming you needed to customize it and aren't using the out of the box SAP framework. If you stash the iView at the bottom, and it's working, you'll see two script tags in the head to your two scripts, and you'll find a script between them calling the google-analytics.com/ga.js script.

Eventually Google will pick up that it's working and you'll begin to track your clicks. Just beware that if you're behind a firewall, you will probably get some strange results as to where your clicks are being routed depending upon your network topology. I've got requests in Ohio showing up as Chicago, which is where the google analytics call is being routed.

ShareThis