ASP.NET MVC: Tracking Page Views and view Duration - asp.net

I have an application I'm currently working on that requires Administrators to be able to track the views of a ticket and it's duration.
I've got the tracking of the number of views by users sorted out, I have a table that contains a TicketID, UserID and a DateOpened. Each time a user visits the page, a new row will be inserted.
However, the way I would like to track the duration of views is by having a DateClosed field in the table which will allow me to work out view duration in the code as opposed to storing the time directly.
I can't use Google Analytics or anything Third Party for the task either due to internal policy as it's an intranet application.
What would be the best way to achieve filling this field on page exit?

Before the window "unloads" simply make an AJAX call.
window.onbeforeunload = function() { //do ajax call }

Related

Hubspot submissions fire twice on Google Tag Manager

I was wondering if someone have had this type of issue when tracking form submissions from a hubspot form.
To give you some context, our client' site is an SPA and has x3 different Hubspot form.
The solution I applied to track form submissions was to create a Custom HTML HubSpot Success Listener Tag, and then I created a look-up table to pass the form ID in a more friendly way.
The issue I'm having is that when I debug this implementation and subscribe to more than one form during the same session, the second submission duplicates:
I know I can configure the tag to fire once per event, instead of once per page. However, I don't want to lose the ability to count a second form during the same session because it's possible a user will want to fill out one form to receive information and another form to arrange a meeting.
Should I get the web developers involved to implement a dataLayer push for each form?
Thanks.
First, you want to debug your existing solution. You don't need GTM for it, though you can still use it. For the debugging, you will want to know what HS returns in their callback on form submission.
Just open your console, paste a listener that would show you the payload coming with it and inspect it:
window.addEventListener("message", function(event) {
console.log(event.data);
});
You will see something like this:
This indicates that we get three callbacks on form submission. You can listen for any of these.
Ah, looks like I'm getting the same form IDs that you have on your screenshot. Now, I'm not sure where that ID comes from. It's likely your developers and not HS are responsible for form IDs. I don't imagine HS could make such a trivial mistake. So ask the devs to change the form ids.
If they can't set unique ids for the forms, then yes, they will have to push custom events there.

Google tag manager resetting events list on page load

We put the GTM code snippet in the site.
However the sequence of events resets every time I switch from one page to another.
For example - if a user comes on the login page, enters the login details and clicks the login button to go to the home page, the list of events reset and I lose all data about previous events.
How can i ensure that I dont lose the event data from previous pages?
There's nothing you can do about the browser resetting window-level variables, that's just how they work.
You might want to reconsider the way you think about GTM and the dataLayer. Instead of thinking of the dataLayer as a list of all past events, think of it as just a way to send data to Google Analytics (or whatever too you're using). Google Analytics is what stores the events, the dataLayer is just an interface for sending them there.
Having said that, here are some potential solutions for remembering values across pages:
You could try using something like window.sessionStorage, which will allow the browser to remember certain values across pages. This isn't a solution for remembering the entire dataLayer, but if you have one or two bits of data you want on multiple pages, this might work.
Alternatively, if you have data that's stored on the server (like the user's ID) that you want to have client-side, you could have your server generate some code to push that data when the page loads, like this:
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
'userId': '123'
});
Edit: I just learned that Simo Ahava found a way to do this. It is very complicated and I still recommend changing the way you think about the dataLayer, but it is possible.

Analytics User-ID view not tracking events

I've created a new view in Google Analytics with everything set up the same as a currently functioning one, only with the addition of User ID tracking turned ON. It's using the same property so the UA code has not changed.
I've copied over everything that I can think of from the current view to the new view. At first glance everything was working fine, page views were coming through via Real-Time correctly and matched up with the values seen in the current view.
For some reason though, the new view is not listing ANY actual events in Real-Time, however the events graph is being populated.
The current view IS tracking all the events correctly.
We use Tag Manager to handle all our events among other things, and using the debug mode, all events were also being triggered correctly. Finally, I've also used the GA Debugger chrome extension, which again is not showing any issues. The new User ID property is being passed through to analytics as I'd expect.
I've tried searching for any issues related to User ID views and tracking events, but came up blank, presumably because there is no inherent issue with this set up.
Any suggestions?
I found out that the reason that the events were not tracking is because the user id was not being sent along with every event.
I believed that once it was set on page load every analytics interaction after that would use that user id. This was not the case. In Tag manager I added a 'Field to set' property for all relevant tags like so;

Display user ID in the metrics of application Insight

This page
http://azure.microsoft.com/en-us/documentation/articles/app-insights-web-track-usage-custom-events-metrics/#authenticated-users
Show how to have login information in the section "Authenticated users".
But how can I see this information in Application Insight after that
I do not see user anywhere in the custom event properties for events, it seems to be the internal field that's collected but not directly exposed in the view because most of the time this "auto-collected" user would be simply an auto-generated GUID....
You can take a look at the users summary on "Usage Analytics/Users" view as well as in Metric Explorer (just select the metrics about app users like "user accounts"...)
You should be able to search for user in the Diagnostic Search but only if you specify the user name you are looking for in the search - you'll get events/traces and so on for that particular user.
Also, you can try to submit authenticated user as a custom property on the event instead of (in addition to) the embedded property, in this case you should definitely see if as part of the properties in UI but this leads to a duplication of the value inside the event (internal + custom)...
You can make a query with Analytics. The result of your query should return a column called user_AuthenticatedId.

Using Google Analytics, if I explicitly log an event using _trackPageview, does that count as a 'real' page view?

For example, if I have a news page that's already being tracked via GA and I add a javascript event to capture clicks on a specific link to the news page (e.g. navigation) am I then 'double counting'?
If a fake pageview is not beneficial in this situation, and from your description that you're looking to track an engagement click on your page, use Event Tracking instead of Pageviews.
Tracking a click/event is easy (especially if you're already using Javascript). The best part is that this event is not considered a page view, keeping those stats safe.
The implementation is simple and allows for quite a bit of customization:
_trackEvent(category, action, opt_label, opt_value)
Below is an example of a link that's been encoded with an event tag:
Play
Here's the Google Analytics resource page on Event Tagging:
http://code.google.com/apis/analytics/docs/tracking/eventTrackerGuide.html
Well it's not a real page view, but Google Analytics thinks that it is--i.e., it shows up in your pageview counts.
Fortunately, it's easy to filter those so they don't contaminate your pageview data.
So for instance,
_gaq.push(['_trackPageview', 'unique_virtual_pagename']);
So in your GA Browser, you'll see the number of clicks actually shown as the number of pageviews of *unique_virtual_pagename*, which is not good.
There are a two ways to fix this (that i am aware of): (i) set a temporary filter at the bottom of the pageview table; or (ii) set a persistent (c*ustom*) filter in your Admin Panel (which i think is best) to remove pageviews having only the name *unique_virtual_pagename*, or whatever name you've chosen. This will of course take up to 24 hours to set, so the best plan is to set the filter first, then add the javascript to your page. so the filter is active when you begin collecting clicks.
But that just solves the problem of disaggregating these virtual pageviews from your real pageviews, you still need a way to count/record them.
I prefer to create a separate profile in these cases. So first, i filter the virtual pageviews from my actual pageviews using a custom filter, then i create a new profile which has another filter excluding everything but these virtual pageviews. I usually give that profile a name based on the event.
What you're doing is registering what's called a 'virtual page view'. To GA it's seen as a real page view and shows up in your content report's and page view counts etc. This is often useful if you want to show a page view for media that GA can't track. It's also commonly used to count an event, such as a button click, as a goal conversion.
If you just want to record the event and not count a page view you should look at using GA event tracking instead.

Resources