Track IP Addresses In Google Analytics

UPDATE: Apparently this is against Analytic’s TOS. So if you are thinking about collecting this data it might be wise NOT to.

Now that Google Analytics allows multiple custom variables it is much easier to track your users ip addresses. Simply insert the following code in your analytics tag:

pagetracker._setCustomVar(1, "IP", "<?php echo $_SERVER['REMOTE_ADDR']; ?>", 2);

To break down each variable:

  • SLOT – 1 refers to the Index or “slot” – you can have up to five slots
  • NAME – IP Address names the variable “IP Address” in Analytics
  • VALUE – The php script echos the visitor’s ip address as the value for the above named variable
  • SCOPE – The final “1” defines the scope of the variable. Possible scopes include:
    • 1 – visit level
    • 2 – session level
    • 3 – page level (default value)

There is a lag of about 12-24 hours before the IP addresses will show in analytics. To access this data click on Visitors->Custom Variables. A variable titles “IP” should appear in the list. Click on it to get detailed statistics. It should look something like this:

 

Google Analytics with IP Tracking

 

Go here to read more about Custom Variables in Google Analytics.

UPDATE: Apparently this is against Analytic’s TOS. So if you are collecting this data it might be wise NOT to.

The applicable section is:

“7. PRIVACY . You will not (and will not allow any third party to) use the Service to track or collect personally identifiable information of Internet users, nor will You (or will You allow any third party to) associate any data gathered from Your website(s) (or such third parties’ website(s)) with any personally identifying information from any source as part of Your use (or such third parties’ use) of the Service. You will have and abide by an appropriate privacy policy and will comply with all applicable laws relating to the collection of information from visitors to Your websites. You must post a privacy policy and that policy must provide notice of your use of a cookie that collects anonymous traffic data.”

PHP & MySQL Keyword Rankings Script

I’ve been working on a Google keyword rankings script that scrapes Google search engine results pages and stores the values in a mysql database. The bulk of the heavy lifting is done via php & cURL. I used some jquery to allow manual updating of rankings. Much of the scraper code was lifted from an old 5ubliminal post that I can’t seem to find anymore. The following video is a short clip of a portion of the script in action. You can test out the script here.

download

rank.zip

Files

  • connect.php – stores credentials for mysql connection
  • add-domains.php – allows you to add a domain into the database. This domain is then presented as an option on the index page.
  • add-keywords.php – checks a domain (already inserted via add-domains.php) against a keyword. If a match is found it inserts the data into the database.
  • index.php – displays all of your domains
  • final.php – the actual keyword rankings page. Allows you to update rankings and see each domain’s keyword rankings on one page.
  • style.css – some css styling
  • postjax2.php – processes serp results and stores values in the db
  • keyword_rankings.sql – database scheme – load this query via phpmyadmin to setup your mysql db
  • header.php – some navigation
  • ajaxproxy.php – some cross domain proxy I found to allow you to get rankings from multiple ip addresses

Google doesn’t particularly like it’s serps scraped – sometimes they will return garbage data if they suspect you are doing it. To get around this, we must bounce our requests off of multiple IP addresses. If you have a few cheap hosting accounts lying around, upload the postajax.php script to a folder accessible from the webserver. Insert your urls where these files can be found into the following section of code:

  
   var csurla = 'http://YOUR_URL_HERE/postjax2.php';
   var csurlb = 'http://YOUR_URL_HERE/postjax2.php';
   function cointoss(wot){
         var d= new Date().getSeconds();
         wot=(wot)? wot: [csurla, csurlb];
         d+= Math.round(Math.random()*10);
         return wot[d%2];
         }

The above example randomly chooses one of two urls to post the data to. This code could easily be changed to accommodate as many urls as you have access to. The next segment of script gathers the data from each table cell in a row of data. We then serialize the data and post it to to our ajax proxy which gets around the pesky cross domain ajax restriction. the $.Ajax command that is built in to jQuery then waits for the php proxy script to respond and then inserts the resulting data into the row we just clicked:

$(".update").click(function() {
       /* extract all the info out of the table cells */
       var selectR = $(this);
       var urlsearch = $(".urlse a").html();
       var se = $(selectR).find("td.se").html();
       var wt = $(selectR).find("td.wt").html();
       var str = $(selectR).find("td.keywords a").html();
       var ranking = $(selectR).find("td.rankingspan").html();
       var orank = $(selectR).find("td.orank").html();
       var delta = $(selectR).find("td.hclass div").html();
       var clicks = $('#tabler').find("td.clickstotal").html();
       var wttotal = $('#tabler').find("td.wttotal").html();
       var keysearch = str.replace('/%20', '+');
       var csurl = cointoss();
       var dataString = 'csurl=' + csurl + '&clicks=' + clicks + '&wttotal=' + wttotal + '&domain=' + urlsearch + '&keyword=' + keysearch + '&wt=' + wt + '&se=' + se + '&rank=' + ranking + '&orank=' + orank + '&delta=' + delta;
       $(this).html("<td colspan='9' align='center'><img src='http://PATH_TO/loading.gif'></td>");

          $.ajax({
          type: "POST",
          url: "http://PATH_TO/ajaxproxy.php",
          data: dataString,
          cache: false,
          success: function(data){
          $(selectR).html(data);
          var totalr = 0;
          var tempint = 0;
          $("td.clicks").each(function(){
          if($(this).text().length > 0){
           var tempint = parseFloat($(this).text()); 
           totalr += tempint;
           }})
          $("#clickstotal").html(totalr);

       $("td.wt").each(function(){
       if($(this).text().length > 0){
           var tempwt = parseFloat($(this).text()); //convert to number
           totalwt += tempwt;
           totalwt = totalwt.toFixed(2);
           }})
          }
          });
      });