I am trying to find a way to register all the traffic of a website with Google Analytics, spliting the reporting based on the Microsoft.AspNet.Identity role.
I did not found anything that allows to do it.
Something like:
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>
}
You are looking for the User ID feature. Here is a Tutorial.
You need to setup the feature. Then you can start tracking like this:
ga('create', 'UA-XXXXXX-Y', {'userId': customUserId});
Anonymous Users you can detect by
User.Identity.IsAuthenticated
You want to send a userid that you do not have like 0
for anonymous users.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With