Google Analytics from LocalHost
If you want to integrate Google Analytics with a site, you want it all working before the site goes live. Even better: integrate it from the convenient, secure localhost server on your dev box.
Prerequisites:
- a gmail account
- a Google Analytics account
- a web server hosting at least one html page on your local PC
Your Google Analytics log-in can be associated with multiple “Accounts” which you define. I organize mine according to what server it tracks and give them the same name as the server machine name. You may decide it is best to segment by customers in order to keep data separated and name it after your customer.
Within an account you define one or more “Properties” which define what you intend to track. If you have a customer with multiple sites or a site and a mobile app you can keep them under a single account using properties to enter distinct URLs etc. I add a property and name it for each site I develop on a given server.
To create a new account you have to create at least one profile which will be the default. Profiles are for filtering reports and data and are not really relevant for localhost tests except that you need to make one to see reports:
- Log into Google Analytics
- Click the Admin tab at upper Right.
- Click Profiles tab.
- Click + New Profile
- Enter profile name and set time zone and country
- Click Create Profile
- Click Property Settings tab
- Enter Property Name. I used my machine name + “” to test a page located at my localhost server’s web root.
- Enter Default URL: I use my site name and end with “.com” You used to be able to enter .localhost but that doesn’t work as I’m writing this.
- Default Profile: the profile you want the Property to be associated with. If you have more than one dev box or image, pick the one that hosts the site.
- I don’t think there is a way to complete a site verification, so I only bother with Webmaster Tools Settings when I move an instance of the site onto a live web server I have already verified.
- Click Apply
Change some settings of the property to work with localhost:
- Click Tracking Code tab
- Scroll down to Website Tracking section
- Click Standard tab
- Under “1. What are you tracking?” tick Multiple top-level domains radio button
- Under “2. Paste this code on your site” copy the code from the box indicated right below and paste in a text editor
- Find the line that reads similar to this: _gaq.push([‘_setDomainName’, ‘[yourSiteName’]);
- Replace with this: _gaq.push([‘_setDomainName’, ‘none’]);
- Paste the final edited code so that it is the last script before the closing head tag in your default site page, such as index.htm.
- I added a button to prompt sending a call to analytics without refreshing the page using the following html:
- Load on your browser and click the button.
Another bonus of working from your own server is that you are in control of the events that occur so the signal to noise ratio is effectively 0. My click is the one and only click today!