How to configure/setup/use multiple Google Analytics Tracking Codes in a Web Application (ASP.NET MVC 5), how to track/collect web applications’s Custom Data in Google Analytics’s Custom Dimensions and how to track events in Google Analytics.

a) How to configure/setup/use multiple Google Analytics Tracking Codes in a Web Application (ASP.NET MVC 5).

Add a googleAnalyticsFunctions.js file to your web application & include it on the pages that you want to track.  Do, update the ‘gaAnalyticsIds’ tracking ids array with your tracking ids.

googleAnalyticsFunctions.js

Results:

As shown below, data collection started successfully for the same web application under multiple tracking ids.

For first Tracking ID

Google Analytics Multiple Analytics Codes

For second Tracking ID

 

Google Analytics Multiple Analytics Codes

b) Book Store Web Application (ASP.NET MVC 5)

We want to collect some Custom Data  (User Id, Book Publisher,  Book Category, Book ID) in Google Analytic when Buy Now event occur.

Application architecture

Application With Google Analytics Tracking Enabled

User Interface

Books Application

Models:

Book Model:

Book.cs

Home Page View Model:

HomePageModel.cs

Home Controller with Index Action Method:

HomeController.cs

Client-side code for handling BuyNow click:

home.js

Index view:

Index.cshtml

Scripts section contains references to googleAnalyticsFunctions.js & home.js.

c) How to create and track data in Custom Dimensions in Google Analytics under multiple tracking ids for same web application.

Setup Custom Dimensions in Google Analytics Account

First of all setup custom dimensions in Google Analytics using the Admin Tab as shown below for all the tracking ids under which you want to track data. Here, four custom dimensions are configured:

‘dimension1’ – User Id,
‘dimension2’ – Book Publisher,
‘dimension3’ –  Book Category,
‘dimension4’ – Book ID

In all 20 custom dimensions can be configured in Google Analytics.

Google Analytics Custom Dimensions

Update the googleAnalyticsFunctions.js with method to send custom data to Google Analytics as shown below

Lastly, update Home.buyNow method in home.js to call the GoogleAnalyticsFunctions.sendEventData method as shown below

So, now when user click BuyNow, Custom Data (User Id, Book Publisher,  Book Category, Book ID)  is send to Google Analytics in respective Custom Dimensions (dimension1, dimension2, dimension3, dimension4) that were set in Google Analytics.

d) How to setup and track events in Google Analytics.

As, shown in the above code in GoogleAnalyticsFunctions.js, event tracking is done using:

Result: Event gets tracked in respective Tracking IDs

For first Tracking ID

Google Analytics Event Tracking

For second Tracking ID

Google Analytics Event Tracking

e) Result can be seen in Google Analytics Query Analyser Tool for the respective tracking ids as shown below

Query parameters

Google Analytics Query Analyser Tool

Query Result

Results In Google Analytics Query Analyser Tool

Happy tracking !!!

Kapil Khandelwal

Web Developer, Blogger, Microsoft Certified Professional (MCP), DZone's Most Valuable Blogger (MVB)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *