Google Analytics Role Based Microsoft.AspNet.Identity

I am trying to find a way to record all website traffic using Google Analytics by splitting the report based on the role of Microsoft.AspNet.Identity.

I did not find anything that allows me to do this.

Sort of:

if(User.IsInRole("public")) { <script> (function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'PUBLIC', 'auto'); ga('send', 'pageview'); </script> } else if (User.IsInRole("private")) { <script> (function (i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'PRIVATE', 'auto'); ga('send', 'pageview'); </script> } 
+5
source share
2 answers

I tested for several days and I found a great way to have 2 different properties in the same account:

 @if (Model.IsUserInternal == true) { <script> (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-XXXXXXXXX-2', 'auto'); ga('send', 'pageview'); </script> } else { <script> (function(i, s, o, g, r, a, m) { i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function() { (i[r].q = i[r].q || []).push(arguments) }, i[r].l = 1 * new Date(); a = s.createElement(o), m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m) })(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga'); ga('create', 'UA-XXXXXXXXX-1', 'auto'); ga('send', 'pageview'); </script> } 

Where XXXXXXXXX is the same.

0
source

You are looking for a user id function . Here is a tutorial .

You need to setup this function. Then you can start tracking as follows:

 ga('create', 'UA-XXXXXX-Y', {'userId': customUserId}); 

Anonymous users you may discover with

 User.Identity.IsAuthenticated 

You want to send a user id that you do not like 0 to anonymous users.

+2
source

Source: https://habr.com/ru/post/1263337/


All Articles