4

I'm new with UTM (Urchin tracking module) and I want to get the UTM parameters where the user clicked the link from (e.g. facebook, google, twitter) and how many time the user clicked the link with same utm source and save it to the mysql database.

I have a PHP snippet but the utm parameters didn't get and save in the database.

How to fix this?

if(isset($_SERVER['HTTP_REFERER'])){
 $utm_source = isset($_GET['utm_source']) ? $_GET['utm_source'] : '';
 $utm_medium = isset($_GET['utm_medium']) ? $_GET['utm_medium'] : '';
 $utm_campaign = isset($_GET['utm_campaign']) ? $_GET['utm_campaign'] : '';
 $utm_term = isset($_GET['utm_term']) ? $_GET['utm_term'] : '';

echo $_SERVER['HTTP_REFERER'];

$sql = "INSERT INTO utm_param(utm_source,utm_medium,utm_campaign)
        VALUES('".$conn->real_escape_string($utm_source)."',
               '".$conn->real_escape_string($utm_medium)."',
               '".$conn->real_escape_string($utm_campaign)."')";
}

utm_param db table structure

Describe utm_param;

User014019
  • 1,215
  • 8
  • 34
  • 66

1 Answers1

6

Before we worry about getting the value of the UTM attributes, you have a security issue that needs some attention. You are currently taking un-sanitised (unsafe) user input (via $_POST['var']) and putting it straight into a database query, which is an unsafe practice that allows SQL injection (e.g. a malicious user can send you a specially crafted string that gives them the ability to do whatever they like to your database).

Please have a read of this Stack Overflow question on how to avoid SQL injection in PHP.

Typically, the standard utm parameters for tracking where a user comes from form part of the query string, for example:

http://yoursite.com?utm_source=google&utm_campaign=campaign_name

Unless you are doing something non-standard, this will be a GET request to your server (as opposed to a POST) so your variables will be available via the $_GET superglobal. For example $_GET['utm_source'] and $_GET['utm_campaign']

Looking at your table schema, you may also need to add auto_increment to your ID column, so that it will get a value automatically when a new row is added.

ALTER TABLE `utm_param` 
CHANGE COLUMN `id` `id` INT(100) NOT NULL AUTO_INCREMENT;
Community
  • 1
  • 1
Matt Renner
  • 433
  • 1
  • 4
  • 7
  • Assuming those values are all set (e.g. the referer value has been sent by the browser, along with utm_x params in the query string), that should work. You will also need a `$result = $conn->query($sql);`, which I assume you have somewhere after the code you've detailed above. – Matt Renner Oct 11 '16 at 07:33
  • yes, i set some dummy values in each utm param but when I checked the database, the values of utm didn't save – User014019 Oct 11 '16 at 07:37
  • Ok - so I'd recommend temporarily `print_r($var)`ing your $utm_ variables before you do the query to make sure they are set, and if they are then check the value of `$result = $conn->query($sql);` to see if the query actually worked. AFAIK `$result` will be true on success – Matt Renner Oct 11 '16 at 07:51
  • the values of utm_variables are displayed correctly when I tried to `print_r` – User014019 Oct 11 '16 at 08:01
  • Cool, so in that case your issue is either with your database schema (structure of tables and columns) or the query. Can you update your question with the output of `DESCRIBE utm_param` to show the table structure (assuming you are using MySQL? – Matt Renner Oct 11 '16 at 08:07