Integrating the Google Analytics API in SharePoint

Tobias Zimmergren
Tobias Zimmergren
💡TIP: Check out the guidance for building sustainable Azure workloads! 🌿

Table of Contents

In one of the projects, we’ve looked at various solutions for providing analytics to users, site owners, and system administrators. Most services offer an excellent way for web admins or site administrators to review how their site or system is being visited – but there are few options for letting the users of your system review specific data themselves.

To get around this, I built on top of the Google Analytics APIs, enabling my application to post queries for analytics data directly to the Google Analytics API. This allows (for example) a site owner to review data specific to his site.

In this post, I will not outline the technical steps but focus on the solution from an architectural standpoint. I may post that later if someone is interested in setting up such a solution.

Google Analytics?

SharePoint already has a lot of built-in Analytics functionality, so why would one want to integrate Google Analytics into your portal/intranet?

First, SharePoint’s analytics isn’t even close to being as advanced as the Google Analytics service, and there’s no good way to compare both. After conducting various studies and trial-and-error runs, the decision falls quickly on Google Analytics.

Just remember that if you are to choose an off-site hosted solution for analytics (or anything else), the data flow over the internet. It would help if you were careful with what information you choose to push into the cloud, whatever the service.

A typical setup for Google Analytics looks like this:

image

In this type of setup, which most people use, the system users get a client script loaded in their browser, which then communicates with the Google Analytics service to store information. Then only the administrators of the Google Analytics service can access and review the data.

We want all users, without the need to authenticate or navigate away from the portal, to be able to review data for their specific sites.

Solution Architecture

Right, we’re running Google Analytics, and we’ve had it integrated into our solutions for a while now – how do we enable GA Administrators to review analytics data and end-users in their own portal?

What we want to achieve is this:

  • Users should be able to see live analytics data of their sites (Owners)
  • Users shouldn’t need to have an account in Google Analytics (ponder that you’ve got thousands of users, you wouldn’t want to manage that…)
  • Users should have the data easily accessible from SharePoint.

Custom WCF Service as a Google Analytics API wrapper

I decided to wrap the Google Analytics API through a custom WCF service where I can expose the specific analytics queries we want to enable for the users. By doing this, we can have our custom service take care of the following:

  • Authentication toward the Google Analytics API’s
  • Contains a set of specific methods to request very specific queries only - This makes it impossible for the user to hijack the request and alter it, should there be a genius out there..
  • Takes the current Site Url as a property for filtering the GA request, only returning data relevant to the specified site URL
  • Enable REST-formatted queries for easy jQuery requests in JSON format

The logic of the solution ended up something like this:

image

Breakdown:

  • The user navigates to a custom Page in SharePoint, just like any other
  • The Page has a jQuery script to request information from our custom WCF service
  • The WCF service first handles authentication et al, then passes on the query to the GA API’s
  • The WCF service, upon receiving the response from Google Analytics, returns the data in JSON
  • The jQuery Page handles the JSON response and neatly pushes it into the Page for the user to review

By utilizing the stand-alone WCF service we could get rid of all the clutter of authentication, configuration and processing of the GA API service and only focus on exposing WCF Methods for the tasks we need, hence enabling a pretty neat asynchronous interface through jQuery for the users.

Summary

The Site Owners are happy because they don’t have to send a ticket or request information from the system administrators as soon as they want to review some data about their sites.

The Administrators don’t have to handle requests to enable site owners to see their statistics.

And yes, with this solution, we enabled many features that the built-in tools couldn’t handle, and we didn’t have to buy any of the expensive third-party offerings for this.

Everyone’s happy, which makes me happy!

Enjoy.

SharePoint

Tobias Zimmergren Twitter

Hi, I'm Tobias! 👋 I write about Microsoft Azure, security, cybersecurity, compliance, cloud architecture, Microsoft 365, and general tech!

Reactions and mentions


Hi, I'm Tobias 👋

Tobias Zimmergren profile picture

Find out more about me.

Recent comments

Mastodon