Entries tagged "JavaScript"

Calculating the Distance between Zip Codes Using the Google Maps API – Part 2

Last week, we went over the basics for accessing the Google Maps API and calculating mileage between zip codes with Google's Distance Matrix service. The problem is that the service only lets us work with 25 origin and 25 destination points at a time. A custom solution is needed to go beyond that. [Continue reading]

Calculating the Distance between Zip Codes Using the Google Maps API – Part 1

A request came in recently to develop a feature for getting a list of organizations near a zip code provided by the user. They're looking for something very similar to a jobs website where you can narrow the search results to show only the ones within a 50 or 100 mile radius. The Google Maps API and Google's Distance Matrix service seem like great solutions for the problem. [Continue reading]

Why PHP_SELF Should Be Avoided When Creating Website Links

When looking for articles about PHP_SELF, it seems like most only refer to the dangers of using the variable with HTML forms. However, there are risks with using it in other parts of a website. For example, it may be tempting to use the variable within the href attribute for links. The problem is that those links become susceptible to Cross-Site Scripting (XSS). Let's take a closer look at the security vulnerability of PHP_SELF and a simple alternative to avoid the problem altogether. [Continue reading]

Generate Usernames with JavaScript: Working with Short Last Names

When generating usernames, one thing to consider is the length of the username. The code from last week's post may be problematic if you're looking for the username to be five characters or more and the user's last name is only two characters. After tacking on the first initial, you would only have three characters. So let's look at getting closer to the desired results. [Continue reading]

Using JavaScript to Dynamically Generate the Username within an HTML Form

Usernames are typically made up of some combination of the user's first and last name. If that's the case, the form used to create those usernames could be modified to take advantage of the data in the first and last name fields. Instead of making someone manually type the username, JavaScript could be employed to generate it automatically. [Continue reading]

Problems with Google Analytics Code for Tracking PDFs

After struggling for nearly a week to install the asynchronous code for Google Analytics, I thought it would be good to share what I've learned. Hopefully this will save you a few sleepless nights.

When I switched to the asynchronous tracking code, everything appeared to be working correctly. But for some reason PDF downloads and visits to external websites weren't showing up in the analytics. According to the Google help documentation (How do I track files (such as PDF, AVI, or WMV) that are downloaded from my site?), I just needed to add an "onclick" event to the downloadable file/external website link:

<a href="/newsletter/2010spr.pdf" onclick="javascript:pageTracker._trackPageview('/newsletter/2010spr.pdf');">

While searching for a solution I stumbled across a Google Chrome plug-in (Google Analytics Tracking Code Debugger) used to validate your tracking code. Using the plug-in, I discovered that the Google help documentation failed to mention one crucial piece of information; you need to define "pageTracker". After a little searching I found another page (Google Analytics Installation Guide) that talks about defining "pageTracker".

With "pageTracker" now defined, the Google Chrome plug-in said that everything was working. But for some reason the code still wasn't working. I used several computers to open PDFs and visit external links. Then I checked Google Analytics an hour or so later to see if they show up. For some reason one of the clicks went through, but that was it. After a few more days of failures and posting on the Google Analytics discussion board I finally realized that the tracking code provided by Google doesn't work.

Instead you want to call the tracking code directly:

<a href="/newsletter/2010spr.pdf" onclick="javascript:_gaq.push(['_trackPageview', '/newsletter/2010spr.pdf']);">

I have no idea why the Google Chrome plug-in said everything was fine, maybe there is a bug on Google's side that prevents the analytics service from accepting results.

Related Links