How do you track clicks on links using javascript and php?

  javascript, php

I need to track clickthroughs on links in a local mysql table. As it is the code I have does nothing but take you to the clicked link.

To keep it simple I have it set so that if you click on the link it takes you to the link rather than a different page.

<a href="trackit.php" onclick="recordClick();">Click this link, then check log.txt</a>

I include jquery in the page. The javascript:

<script type="text/javascript">
function recordClick() {
  $.get("record-clicks.php?ip_address=123.456.789.000&url=trackit.php");
  return false;
}
</script>

The record clicks page is basic just to see if it works:

<?php
$visitor_ip = $_POST['visitor_ip'];
$visitor_ip = $_GET['visitor_ip'];
$url = $_GET['url'];

$fpi = fopen("log.txt","w");
fwrite ($fpi,"successn");
fwrite ($fpi,"visitor_ip: $visitor_ip | url: $urln");
?>

The idea is that you click on the link and it sends the ip address and some other info the the record.php page. Once I get it to pass the data to the record page I’ll set up the mysql and table.

Source: Ask PHP

LEAVE A COMMENT