Naren Verma
Naren Verma

Reputation: 2307

Capturing UTM Parameters Across Multiple Pages

I have some concerns about the UTM parameters.

Issue: Users coming from links with UTM parameters can submit the form, and I can capture the UTM values in the database. However, what happens if they navigate to another page before submitting the form and then submit it from another page then I am getting organic text instated off UTM value?

What I have tried: I created a session to store the UTM values. If users arrive through a link with UTM parameters, I retrieve the UTM values from the session. Otherwise, I get a static text value that I added in the code.

I tried the following code, but it's not working correctly. When I refresh the page without UTM parameters, such as this link: https://www.example.in/testing-form, I get the expected organic text.

However, if users come from a link with UTM parameters, such as https://www.example.in/testing-form?utm_source=linkedin&utm_medium=testcampaign, I still get the same orgainctext instead of the UTM parameter values.

function record_utms(){ 
if (!session_id()) {
 session_start();
}
//checking if the session is exist or not
    $utm_source = isset($_SESSION['utm_source']) ? $_SESSION['utm_source'] : null;
    $utm_medium = isset($_SESSION['utm_medium']) ? $_SESSION['utm_medium'] : null;
    $utm_campaign = isset($_SESSION['utm_campaign']) ? $_SESSION['utm_campaign'] : null;
    $utm_term = isset($_SESSION['utm_term']) ? $_SESSION['utm_term'] : null;

    // If the session does not exist then set session if the URL have utm fields else organic
    if ($utm_source === null) {
        $utm_source = isset($_REQUEST['utm_source']) ? $_REQUEST['utm_source'] : 'organic';
    }
    if ($utm_medium === null) {
        $utm_medium = isset($_REQUEST['utm_medium']) ? $_REQUEST['utm_medium'] : 'website';
    }
    if ($utm_campaign === null) {
        $utm_campaign = isset($_REQUEST['utm_campaign']) ? $_REQUEST['utm_campaign'] : 'webpage';
    }
    if ($utm_term === null) {
        $utm_term = isset($_REQUEST['utm_term']) ? $_REQUEST['utm_term'] : '';
    }

//asign session here
        $_SESSION['utm_source'] = $utm_source;
        $_SESSION['utm_medium'] = $utm_medium;
        $_SESSION['utm_campaign'] = $utm_campaign;
        $_SESSION['utm_term'] = $utm_term;
     

// below is the script to update the utm parameters value
        if(isset($_SESSION['utm_source'])){ 
            echo '<script type="text/javascript">
                    var utm_source = jQuery(".custom-zoho-form #utm_source");
                    var utm_medium = jQuery(".custom-zoho-form #utm_medium");
                    var utm_campaign = jQuery(".custom-zoho-form #utm_campaign");
                    //var utm_term = jQuery(".field_utm_term input");
                    if(utm_source){
                        utm_source.val("'.$utm_source.'");
                        utm_medium.val("'.$utm_medium.'");
                        utm_campaign.val("'.$utm_campaign.'");
                        //utm_term.val("'.$utm_term.'");
                    }
            </script>';
    }
    
    }
    add_action('wp_footer', 'record_utms');

Upvotes: 0

Views: 48

Answers (0)

Related Questions