Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 568077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:09:04+00:00 2026-05-13T13:09:04+00:00

Yes, I KNOW about Google Analytics. We use it for our overall site metrics,

  • 0

Yes, I KNOW about Google Analytics. We use it for our overall site metrics, and I know we can track individual links. However, we needed a tracking solution for very specific links and we need that tracking data available to our web application in real time, so I wrote my own solution:

jQuery:

  $.fn.track = function () {
    var source, url, name, ref, $this;
    $this = $(this);
    if (window.location.search.substring(1) != '') {
      source = window.location.pathname + "?" + window.location.search.substring(1);
    } else {
      source = window.location.pathname;
    }
    url = jQuery.URLEncode($this.attr('href'));
    name = $this.attr('name');
    ref = jQuery.URLEncode(source);
    $this.live('click', function (click) {
      click.preventDefault();
      $.post('/lib/track.php', {
        url: url,
        name: name,
        ref: ref
      }, function () { window.location = $this.attr('href'); });
    });
  };

… using the jQuery URLEncode plugin (http://www.digitalbart.com/jquery-and-urlencode/).

Now, this code works fine with my PHP backend and on my machine, but it doesn’t seem to work reliably for everyone else. Sometimes the parameters passed in via jQuery are NOT passed in, resulting in a record in the database with no name, url or ref.

For the life of me, I can’t figure out why this might be happening; I know the $.post is triggering, since there are records in the database (in the PHP, I also record the IP of the request along with the timestamp), but in many cases the PHP script is receiving blank $_POST variables from jQuery.

I’ve tested it live on every browser I have access to at my workplace, and all of them work fine for me; however, about 75% of all the records created (not by my computers) come through as blank (most of them are using the same browsers I am).

Why could this be happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:09:04+00:00Added an answer on May 13, 2026 at 1:09 pm

    I think, in the end, my problem ended up being that it was taking too long for the request to be parsed by jQuery, and I’m pretty adamant about not wanting to make the links “dependent” on javascript (either that they wouldn’t work without it or that the user would have to wait for the tracking request to complete before they hit the new page).

    After browsing many other solutions online–borrowing from some and being inspired by others–I arrived at the solution below in native javascript:

    if (document.getElementsByClassName === undefined) { // get elements by class name, adjusted for IE's incompetence
        document.getElementsByClassName = function(className) {
          var hasClassName, allElements, results, element;
    
            hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
            allElements = document.getElementsByTagName("*");
            results = [];
    
            for (var i = 0; (element = allElements[i]) !== null; i++) {
                var elementClass = element.className;
                if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass)) {
                    results.push(element);
                }
            }
    
            return results;
        };
    }
    
    function addTracker(obj, type, fn) { // adds a tracker to the page, like $('xxx').event
      if (obj.addEventListener) {
        obj.addEventListener(type, fn, false);
      } else if (obj.addEventListener) {
        obj['e' + type + fn] = fn;
        obj[type + fn] = function() {
          obj['e' + type + fn]( window.event );
        };
        obj.attachEvent('on' + type, obj[type + fn]);
      }
    }
    
    function save_click(passed_object) { // this function records a click
      var now, then, path, encoded, to, from, name, img;
    
      now = new Date();
      path = '/lib/click.php';
      from = (window.decode) ? window.decodeURI(document.URL) : document.URL;
      to = (window.decodeURI) ? window.decodeURI(passed_object.href) : passed_object.href;
      name = (passed_object.name && passed_object.name != '') ? passed_object.name : '[No Name]';
    
      // timestamp the path!
      path += '?timestamp=' + now.getTime();
    
      path += '&to=' + escape(to) + '&from=' + escape(from) + '&name=' + name; // compile the path with the recorded information
      img = new Image();
      img.src = path; // when we call the image, we poll the php page; genius!
    
      while (now.getTime() < then) {
        now = new Date(); // resets the timer for subsequent clicks
      }
    }
    
    function get_targeted_links(target) { // finds targeted elements and wires them up with an event handler
      var links, link;
      if (document.getElementsByClassName) {
        links = document.getElementsByClassName(target);
        for (var i = 0; i < links.length; i++) {
          link = links[i];
          if (link.href) {
            addTracker(links[i], 'mousedown', save_click(links[i])); 
          }
        }
      }
    }
    
    addTracker(window, 'load', get_targeted_links('trackit'));
    

    … which seems to be much snappier than the jQuery plugin I had written above, and so far has been fast enough to track all the requests I’ve thrown at it.

    Hope that helps someone else!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 273k
  • Answers 273k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can select which hunks to add in a commit… May 13, 2026 at 2:09 pm
  • Editorial Team
    Editorial Team added an answer Give your unit names DIFFERENT names, you can then simply… May 13, 2026 at 2:09 pm
  • Editorial Team
    Editorial Team added an answer It sounds as if you are missing a reference to… May 13, 2026 at 2:09 pm

Related Questions

How can I cache a Reference Property in Google App Engine? For example, let's
Everything I'm finding via google is garbage... Note that I want the answer in
I know this is not a straight up question, so if you need me
I've come to realize that several questions I asked in the past, such as
I've written a setup.py script for py2exe, generated an executable for my python GUI

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.