Welcome Guest, Not a member yet? Register   Sign In
Counting outgoing clicks.
#1

[eluser]codelearn[/eluser]
This may not be a directly CI related question, but the community here is smart so I figured this would be a good place to ask.

How would I be able to count (using any method, JS/PHP/HTML, whatever) outgoing clicks on links to websites away from my own domain. For instance I have a link to http://www.codeigniter.com on my site and want to see how many people have clicked on it.

Is there an elegant way to do this?

Thanks!
#2

[eluser]Michael Wales[/eluser]
I would use jQuery (or some other library) to create an AJAX request when a user clicks on an anchor element. That ajax request would call one of your controllers and pass the href of the link as a POST variable, which would then be stored in your database.
#3

[eluser]PauloBr[/eluser]
To store in a database, I think that one of the ways is to make an address that will serve as a "bridge", where you will increment the counter and then the redirect.

Like this:

Code:
http://www.yoursite.com/redirect/www.codeigniter.com/

On redirect controller, you make your count method.

Just my opinion. ;-)
#4

[eluser]ELRafael[/eluser]
Gosh @PauloBr! Too many work hum?

Why don't you use like Michael told? In prototype you have a function to "observe" an element in HTML. Use in "a" element. If href is to another domain (if (href != $_SERVER[forget_the_key_for_domain])), put in database the link and the href.

Hey, Google Analytics do this for you.
#5

[eluser]Michael Wales[/eluser]
@PauloBr
That only really works for links to domains, otherwise you have to do a lot of funky segment counting and concatenating
Code:
// URL is: http://www.domain.com/redirect/http://www.michaelwales.com/2007/10/erkana-codeigniter-authorization-library/
// Redirect to: http://www.michaelwales.com/2007/10/erkana-codeigniter-authorization-library/
$url = '';
for ($i = 2; $i <= $this->uri->total_segments(); $i++) {
    $url .= $this->uri->segment($i) . '/';
}

// Now determine if the original URL had a trailing slash or not
// If not, rip off the one we added
if (substr($this->uri->segment($this->uri->total_segments), -1) == '/') {
    $url = substr($url, 0, -1);
}

// Now redirect to $url;
#6

[eluser]codelearn[/eluser]
Is there a way to do it without enormous JS libraries?
#7

[eluser]Michael Wales[/eluser]
Most of the JS libraries aren't that large, but you could always code it up yourself in JavaScript.

Personally, I would use a library as it allows you to use CSS/XPath selectors, so it's not in-line JavaScript (which we call obtrusive) as well as handling all of the browser compatibility (like how IE and Firefox handle HTTP Requests differently).




Theme © iAndrew 2016 - Forum software by © MyBB