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

DAP breaks when running locally #16

Closed konklone closed 9 years ago

konklone commented 9 years ago

The DAP code breaks when the site is running at http://localhost or http://127.0.0.1, which is the base URL many developers and designers are using when working on making/editing websites.

The error shows up in the developer console like this:

screenshot from 2015-04-11 16 23 26

And it comes from this line:

_mHostName = document.location.hostname.match(/(([^.\/]+\.[^.\/]{2,3}\.[^.\/]{2})|(([^.\/]+\.)[^.\/]{2,4}))(\/.*)?$/)[1];

The script attempts to figure out the "hostname" part of the URL through a complicated regex, followed by an array access. This line is brittle, because if the regex doesn't match on a "traditional" URL, then the resulting match won't have a [1]'th element to access, and you get the error above. Because it's a fatal error, this ceases further JavaScript execution of the remainder of the DAP snippet.

Practically speaking, this means that the DAP script doesn't run in many development environments, which means that website authors don't get a chance to observe DAP's behavior until the code and site is accessible at a traditional URL. Because developers pay closest attention to script and site behavior in development, this means that first- and second-order effects of the DAP will go missed.

As an example, this is partly why I only noticed the behavior in #13 and #15 just now, after a teammate identified the youtube.com hit on the live site, even though I'd never seen it during development.

smarina04 commented 9 years ago

@konklone is this still an issue in 1.04?

konklone commented 9 years ago

This appears to be resolved!