title: Tracking Email Clicks in PHP
body: <p>I am using SwiftMailer to send emails to people who are registered on my website. I want to track opens &amp; click throughs, and I have the opens working perfectly. The click throughs are almost there...</p>
  
  <p>When you open the email I use an image set to 1px by 1px that loads a PHP script. This tracks the person's email address and records the number of times they've opened it in my MySql database. All of the links in the email first take the user to a script on my site that uses GET requests to read their email address and the email that the link was contained in. This script then records this information in my DB.</p>
  
  <p>The problem is that when you click on a link in the email, it records the first click just fine. And it will record every <em>different</em> link a user clicks on, but it will only record each click from the same source once.</p>
  
  <p>IE: In the footer of each email is a link to like the company on Facebook, and another one for Twitter. When you click on FB, the DB is updated to say you clicked on 1 link. If you click on FB again, it will not update. But if you click on Twitter, it will say you clicked on 2 links.</p>
  
  <p>If I load the script directly in my browser it will keep track of each click properly.</p>
  
  <p>EDIT:</p>
  
  <p>URL structure: <code>https://&lt;my website&gt;.com/emails.php?type=click&amp;email=&lt;id of email&gt;&amp;address=&lt;user email&gt;&amp;url=&lt;link to click on&gt;</code></p>
  
  <p>PHP:</p>
  
  <pre><code>if($_GET['type'] == 'click') {
      $email = $_GET['email'];
      $address = $_GET['address'];
      $url = $_GET['url'];
  
      // Get Worker Email
      $sql = mysqli_query($con, "SELECT * FROM worker_emails WHERE id='$email'");
      $row = mysqli_fetch_array($sql);
  
      // Unserialize Array
      $array = unserialize($row['clicks']);
  
      // Update Array
      $array[''.$address.'']['Clicks'] = $array[''.$address.'']['Clicks'] + 1;
  
      // Serialize Array
      $array = serialize($array);
  
      // Update Click Tracking
      mysqli_query($con, "UPDATE worker_emails SET clicks='$array' WHERE id='$email'");
  
      // Send User to Link
      header("Location: ".$url);
  }
  </code></pre>
  
  <p>Suggestions?</p>