Dynamics NAV and Google Analytics – Part 1/2

Part 2 is now here

Google Analytics is an amazing tool for tracking traffic on web pages and mobile apps. It allows you to see visitors in real time, and be able to see exactly what they are viewing.  The only issue is that since it is tailored to websites and mobile apps, it is difficult to get working in desktop applications. In this two part tutorial we are going to integrate Dynamics NAV with Google Analytics. In the first part we will look at setting up the Google account properly and creating the .NET add-in we need to interface with Google’s servers. In the second part we will create a code unit wrapper around our add-in. Then using the hooks pattern to add in code snippets for our event tracking so we minimize the impact on the existing code base.

Google Account Setup

I would suggest making a company account for this since you might have stakeholders or other business users wanting to look at the data. To setup a Google Analytics account, follow the steps outlined here. The main thing you need from this is your tracking ID. It’s usually found in the form of UA-XXXX-Y once you create a property. You may also want to setup some charts or graphs on the data you want to show. In this tutorial we are using the event tracking, so I would suggest a chart that shows event categories and labels.

Research time

So now that we have our account set up. We need to figure out what to send to Google. For this we are going to use their Measurement Protocol. Using HTTP we can send Google our hits and be able to track what we need to. You will want to have a good read over the Protocol Reference and the Parameter Reference. These two documents outline what needs to be sent over HTTP and how to properly format everything. Make sure you fully understand the different parameters and figure out what ones would benefit your business workflows the most. For this tutorial we are just going to focus on event tracking.

.NET Add-in

This add-in is very simple. It takes a key-value list of arguments, forms it into a query string, and sends it off to Google. First, lets setup all the required information we need in our hit. The mandatory fields are protocol version (v), tracking id (tid), client id (cid) and hit type (t). We will assign hit type later in our constructor.

Next we need to make our constructor for tracking an event.

We need to send along the hit type, in this case “event”. For an event we must send along the category and action. When we make our code unit wrapper, we will be passing along the page name as our category and label will be the action name. We will also set value to 1, to signify that it was clicked once.

Now that we have our hit created we can build the base values.

The reason I have this separated out from the constructor is so that you can reuse the  AddBaseValues() with other hit types. And this allows for more finer control over what you are sending to Google.

Next a little helper method. This guy is there to make sure we don’t add in the same parameter twice when building our query. You could also do some more validation and checking with this method if need be.

Finally the sending. This method will add in our base values, format and send off our hit.

Your C# class is done!  If you want to test this out you can make a simple C# console app and try out some values. Here is an example call where the user has the “Vendor Card” open and they clicked on the delete action.

That’s it for now. Spend some time determining what you want to track and how you want to send it to Google. Add any methods that you think you might need. There are lots of things that Google will let you track. Don’t forget about custom dimensions and metrics, as they can let you track pretty much any value you wish.

In part 2 we will be making the Dynamics NAV codeunit wrapper to use our new C# add-in.

 

Leave a Comment