Check UTM tags in magento url

Check UTM tags in magento url

September 9, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Check UTM tags in magento url

Tracking UTM Parameters in Magento: A Comprehensive Guide

UTM parameters are crucial for understanding how customers find your e-commerce store and which marketing campaigns drive conversions. In this post, we’ll explore how to track UTM parameters in Magento using observers and save the data to both customer sessions and order objects.

Understanding the Problem

You’ve already implemented an observer that tracks UTM parameters from referrer URLs when a customer visits your site. You’re checking if the UTM tags are present in the URL, and if so, you’re serializing the parameter data and storing it in the customer session.

However, you’re facing a discrepancy between Google Analytics data and your Magento database. While Google Analytics shows around 50 orders with UTM parameters, your Magento database only contains 2-4 orders with this information.

Let’s dive into why this might be happening and how to address it.

Checking the Database Structure

First, ensure that your sales_flat_order table has columns for the required UTM tag fields (e.g., utm_source, utm_medium, etc.). If not, you can add them using a SQL query like this:

ALTER TABLE sales_flat_order
ADD utm_source VARCHAR(255);
ADD utm_medium VARCHAR(255);
ADD utm_term VARCHAR(255);
ADD utm_content VARCHAR(255);
ADD utm_campaign VARCHAR(255);

Optimizing the Observer

Let’s optimize your existing observers to ensure they consistently save UTM data.

Controller Action Observer

In your controllerActionPredispatch method, you’re already checking if UTM parameters are present and saving them to the customer session. This approach is correct as it ensures that the UTM data persists across multiple pages within a single session.

However, there’s a potential issue with the referrer URL check:

if($reffer_url && strstr($reffer_url,'mysite') === false){

This condition will only allow UTM data to be saved if the referrer URL does not contain ‘mysite’. If this is not intended, you can remove or modify this condition.

Sales Order Observer

In your salesOrderPlaceAfter method, you’re trying to save UTM parameters to the order object. The issue here seems to lie in the if($referrer_data) condition:

$referrer_data = unserialize(Mage::getSingleton('customer/session')->getReferrerData());
if($referrer_data){
    // Rest of your code...
}

If this condition fails, no UTM data will be saved to the order object. To troubleshoot this:

  1. Check if the unserialize method is returning false or an empty array due to a failed serialization/deserialization process.
  2. Verify that the getReferrerData() method actually returns serialized data by checking the session before the observer is triggered.

Potential Solutions

Solution 1: Use a Separate Session for UTM Data

Currently, you’re using the standard customer session to store UTM data. However, this might not be reliable if the customer’s Magento session expires or is cleared due to inactivity.

Consider creating a separate session just for storing UTM parameters that persists longer than a typical customer session (e.g., 24 hours).

Solution 2: Implement a Database Table for UTM Data

Rather than relying on the customer session, you could create a dedicated database table to store UTM data. This approach would provide more stability and allow you to easily query historical UTM data.

The table structure could look like this:

CREATE TABLE utm_data (
  id INT PRIMARY KEY,
  order_id INT NOT NULL,
  utm_source VARCHAR(255),
  utm_medium VARCHAR(255),
  utm_term VARCHAR(255),
  utm_content VARCHAR(255),
  utm_campaign VARCHAR(255),
  created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);

Solution 3: Debugging the Session Data

To diagnose why your getReferrerData() method might not be returning data, you can add some debugging code to log the session data:

->log('Customer session: '.print_r(Mage::getSingleton('customer/session')->getSessionData(true), true));

This will help you understand if the referrer_data is being stored correctly in the session.

Conclusion

Tracking UTM parameters in Magento can provide valuable insights into your marketing effectiveness. While your current implementation is a solid starting point, there are several potential reasons why data might not be consistently saved to the order object:

  1. Issues with the referrer URL check condition.
  2. Problems with serializing/deserializing session data.
  3. Session expiration or clearing due to inactivity.

To resolve these issues:

  1. Review and optimize your observers, ensuring that UTM data is being correctly stored in sessions and saved to order objects.
  2. Consider using a separate session for UTM data if current sessions are unreliable.
  3. Implement a dedicated database table for storing UTM data for better stability and queryability.

By following these best practices and closely examining your implementation, you should be able to consistently track and save UTM parameters in Magento, bridging the gap between Google Analytics data and your database.

For more advanced strategies on tracking customer behavior and optimizing marketing campaigns, check out our guide on Advanced Customer Segmentation Techniques for E-commerce.

Happy coding!

Database Performance Extensions Orders

Our network