digital-analytics-program / gov-wide-code

Provides a set of javascript files and documentation to implement web analytics on US federal websites
http://www.digital.gov/dap
104 stars 55 forks source link

Document auto-tracking functionality in README #47

Closed toolness closed 1 year ago

toolness commented 8 years ago

This adds the auto-tracking code verbatim from a PDF @tdlowden sent me, as a way to document current solutions/workarounds for #45.

toolness commented 8 years ago

Ok, so I added an edit to the original PDF that guards the _initAutoTracker() call, only running it if it's actually been defined. This is because adding support for DAP to be loaded asynchronously (#28) means that it's possible _initAutoTracker() may not be defined at the time that links have been dynamically added to the page. That's fine, though, since DAP will automatically call _initAutoTracker() when it finishes loading.

toolness commented 8 years ago

So I still have some concerns about this particular solution. In the case of pages like calc.gsa.gov, users can enter multiple search queries during the course of a single page's lifetime; the page uses an ajax API to dynamically load results, which ultimately results in the creation of new links. So ideally we'd call _initAutoTracker() every time new results are loaded.

However, based on the current DAP code, it looks like _initAutoTracker() iterates through all links on the page and adds event handlers to the ones that match the criteria for auto-tracking. While this does mean that the newly-added links will be found and auto-tracked, it also means that old links on the page--for instance, a hypothetical mailto: link in the footer--will be auto-tracked multiple times. This means that when someone clicks said mailto: link, the page would tell Google Analytics to track multiple clicks on the link, rather than just one.

While it's entirely possible that Google Analytics will throw away the extra hits, since they're identical and so obviously executed faster than any human could possibly muster, it might not... I think it also constitutes a memory leak, albeit a fairly minor one.

Changing _initAutoTracker() to take an optional array or NodeList of links--defaulting to all links on the page if none is provided--could fix this, by giving the client the ability to provide only the links it knows are new.

toolness commented 8 years ago

Hmm, so it looks like at the very least, the GA client doesn't "debounce" identical events that happen in rapid succession. If you go to calc.gsa.gov, open the JS console and execute _initAutoTracker() a bunch of times, clicking on an outbound link (such as the "View our code on GitHub" link in the footer) just once results in multiple network requests being sent to GA. Even in the best case--if GA "debounces" the events on its server--we're still saturating the user's network connection unnecessarily. 😞