Custom cross domain metric tracking system implementation in php

D1G1T4L

[H]ard|Gawd
Joined
Feb 1, 2001
Messages
1,212
I need to design a website system where users create an account, register their website and get a custom tracking code that display an image/link. I would love to hear some ideas.

The snippet needs to be able to do the following:

a) Track the amount of unique impressions the website gets.

b) Track user visit duration (this need to be precise in seconds until the user leaves the website/domain)

c) Have a tracking snippet code display an clickable image (I should be able to change this image based on which site is showing the snippet).

d) Be able to change the url/link of where the visitor will go if they click the image. (When a visitor comes to the site, the snippet needs to display an image of my choice and link of my choice).

e) Track the clicks of this image.


Anyone know of the most optimalway to do this?
 
You'd love to hear some ideas about what?

You're on for a long search if you want to find the most optimal way.

You should first come up with a data model -- how are you going to lay out the entities and attributes you need to track in physical tables in the database? What indexes will they need?

Once you've got that straight, you can start coding. Accept user input, scrub it, and let the user log in. Once they're logged in, let them alter the database to represent their choices -- change target URLs, set redirects and images, collect their statistics. When URLs are hit, you can use a redirect that records the URL used and then pushes the user to the desired location.

Sounds like you've already chosen PHP. Why is that? What DBMS will you use? How much load do you expect you'll have?

Did you have more specific questions?
 
I have the database set up. I am just trying to understand the mechanics of the following

1) How would I actually track the user duration on other people's site from technical perspective.
2) How would this snippet look like? (javascript/html5? This snippet needs to display an image on their page of my choice and be able to track the click. How is that that done technically?)
 
From past experiences, you would need to create a external js file that the user would have to put on all their web pages. When the document is loaded, you would start trackins various things. EX: Every second or two, you would use a ajax request to your server that the user is on the page.

As far as what is clicked, I've seen code that wires to mostly all events on the DOM. jQuery being the easiest to do it with, but button clicks, links, mouse move, etc.. and an ajax request back to the server with info about it EX: client IP, what was clicked, destination, etc...

I'm not going to write it out, but it is fairly easy these days to track assuming the user has javascript enabled and your system can handle the number of requests (think server farm with load balancing).

using jquery:
Code:
<script>
    $().ready(function() {
        $(":button").click(function() { 
            alert($(this).attr("id")); 
        });

        $(":button").click(function() { 
            alert("A button was clicked");
        });

        $("a").click(function() { 
            alert($(this).attr("href"));
        });
    }
</script>
 
Last edited:
From past experiences, you would need to create a external js file that the user would have to put on all their web pages. When the document is loaded, you would start trackins various things. EX: Every second or two, you would use a ajax request to your server that the user is on the page.

As far as what is clicked, I've seen code that wires to mostly all events on the DOM. jQuery being the easiest to do it with, but button clicks, links, mouse move, etc.. and an ajax request back to the server with info about it EX: client IP, what was clicked, destination, etc...

How would you do a cross domain ajax request? Also javascript doesn't have direct knowledge of a user's IP address.
 
From past experiences, you would need to create a external js file that the user would have to put on all their web pages. When the document is loaded, you would start trackins various things. EX: Every second or two, you would use a ajax request to your server that the user is on the page.

As far as what is clicked, I've seen code that wires to mostly all events on the DOM. jQuery being the easiest to do it with, but button clicks, links, mouse move, etc.. and an ajax request back to the server with info about it EX: client IP, what was clicked, destination, etc...

I'm not going to write it out, but it is fairly easy these days to track assuming the user has javascript enabled and your system can handle the number of requests (think server farm with load balancing).

using jquery:
Code:
<script>
    $().ready(function() {
        $(":button").click(function() { 
            alert($(this).attr("id")); 
        });

        $(":button").click(function() { 
            alert("A button was clicked");
        });

        $("a").click(function() { 
            alert($(this).attr("href"));
        });
    }
</script>

Just lurking through - a side note.

I don't think your examples above are very good. They create as many event handlers as the selection size, I think, which could be hundreds / thousandths or more. Something like:
Code:
$('body').on('click','a',function(e) {
  // do stuff
});
...Would be much better, as it creates only one event handler. This also adds the benefit of not needing to wait for DOM ready event.

I think $('body') selector can also be replaced by $(document). At that point you could just place the above in <head> and not worry about the actual DOM elements at all.

How would you do a cross domain ajax request? Also javascript doesn't have direct knowledge of a user's IP address.

I think you can use JSONP for those requests, although I have never used it, so I can't say much more. You also don't need that user's IP address. You only want to uniquely identify that user, no? That unique id doesn't have to be an ip address. Google this stuff - there's some interesting stuff out there for potential solutions.
 
Just lurking through - a side note.

I don't think your examples above are very good. They create as many event handlers as the selection size, I think, which could be hundreds / thousandths or more. Something like:
Code:
$('body').on('click','a',function(e) {
  // do stuff
});
...Would be much better, as it creates only one event handler. This also adds the benefit of not needing to wait for DOM ready event.

From my understanding, your code is no different than mine. It is still a selector that selects ALL the anchor tags and wires the click event of each that to it. I guess I kinda see the point of selecting the body, but I don't know the performance implications, if any, that would occur from selecting the body verus the document.

As for the DOM ready event, it depends where you put your js file reference. If you put it in the head, it will load the file before the entire document is ready meaning that the JS may not work correctly. If the reference is at the bottom of your page, which I recommend, then you don't need to wait as the page will be loaded before the js file is downloaded.



As for the client IP, when the user does something you want, you would send a request to your server from the client brower. You could then catch the client IP from the request in your server side code. I imagine you are using the IP for geography tracking.
 
Last edited:
Much of this thread sounds like it is reinventing the wheel when compared to products from Google, Omniture, etc. Here's some of my thoughts and questions...

- What is the definition of a "unique impression"? What rules must pass before something is deemed a "unique impression"?
- Why do you need the total visit time with precision to seconds? Google Analytics doesn't even use this level of precision. The value of this level of precision (versus "to the minute") seems subjective when compared to people multitasking within multiple browser instances/tabs.
- By what standards is something deemed "most optimal"?
- Why PHP? Is this technology truly parallel with your desire for the "most optimal"?
- Tracking by IP can be done, but the complicated part is differentiating between a single user versus multiple users behind the same public IP.
- Click tracking is easy to implement. But would the data you collect have to be reported to a third party? Seems like the concept of "conversion" may have been missed or ignored. (Also see the previous point.)
- How do you foresee someone implementing your widget on their site? And why would someone implement the widget on their site? Or am I mis-understanding the user base of this widget?
 
Last edited:
From my understanding, your code is no different than mine. It is still a selector that selects ALL the anchor tags and wires the click event of each that to it. I guess I kinda see the point of selecting the body, but I don't know the performance implications, if any, that would occur from selecting the body verus the document.

No. The example that I gave creates one event handler, on the body element. I looked up jQuery's documentation - the proper term for this technique is delegated events. Selecting $(document) in a case like this is also a-okay, and it would allow to place the script in the <head> without waiting for DOM ready.

As for the DOM ready event, it depends where you put your js file reference. If you put it in the head, it will load the file before the entire document is ready meaning that the JS may not work correctly. If the reference is at the bottom of your page, which I recommend, then you don't need to wait as the page will be loaded before the js file is downloaded.

Putting scripts in a body creates problems, too, especially with dynamically loaded content (OP's problem is a good example). Let's say that you have an Ajax-based application. On hash or history change, you load up another document. Do you want to evaluate all of the scripts coming from that document? There's a good chance that there are scripts that have been evaluated already, which can cause problems. If OP was to have a tracking script, the tracking script could potentially re-run itself, causing duplicate requests further on. Having a giant script footer = bad.
 
No. The example that I gave creates one event handler, on the body element. I looked up jQuery's documentation - the proper term for this technique is delegated events. Selecting $(document) in a case like this is also a-okay, and it would allow to place the script in the <head> without waiting for DOM ready.



Putting scripts in a body creates problems, too, especially with dynamically loaded content (OP's problem is a good example). Let's say that you have an Ajax-based application. On hash or history change, you load up another document. Do you want to evaluate all of the scripts coming from that document? There's a good chance that there are scripts that have been evaluated already, which can cause problems. If OP was to have a tracking script, the tracking script could potentially re-run itself, causing duplicate requests further on. Having a giant script footer = bad.

What would be a good place to put the snippet code into?
 
No. The example that I gave creates one event handler, on the body element. I looked up jQuery's documentation - the proper term for this technique is delegated events. Selecting $(document) in a case like this is also a-okay, and it would allow to place the script in the <head> without waiting for DOM ready.



Putting scripts in a body creates problems, too, especially with dynamically loaded content (OP's problem is a good example). Let's say that you have an Ajax-based application. On hash or history change, you load up another document. Do you want to evaluate all of the scripts coming from that document? There's a good chance that there are scripts that have been evaluated already, which can cause problems. If OP was to have a tracking script, the tracking script could potentially re-run itself, causing duplicate requests further on. Having a giant script footer = bad.



After re-reading the documentation, you are correct sir. Now I know something new :), thanks.

Put the snippet in the head tag
 
Back
Top