Grow your CSS skills. Land your dream job.

If/Else statement, different analytics code depending on URL

  • # December 8, 2009 at 12:39 pm

    Thanks ahead of time for help on this topic. I’ve searched through the forum, and couldn’t find anything similar, so forgive me and point me in the right direction if there’s an answer.

    I’m trying to right a php if/ else statement that needs to go in between the <head> tags of the main template page.
    Depending on the URL, I need different analytics code to be shown in the header to run javascript to track conversions.

    Example:

    If it’s this url, http://test.tester.local/customer, run this analytics code:

    <SCRIPT language="JavaScript" type="text/javascript">
    <!– Yahoo! Inc.
    // Event Type ID: 1
    // Segment Type ID: 0
    if (typeof(window.ysm_customData) != ‘object’) window.ysm_customData = new Object();
    window.ysm_customData.segment_1SE66WWWWP06H0 = "event=0,transId=,currency=,amount=";
    // –>
    </SCRIPT>

    else if it’s this url, http://test.tester.local/documents, run this analytics code:

    <SCRIPT language="JavaScript" type="text/javascript">
    <!– Yahoo! Inc.
    // Event Type ID: 1
    // Segment Type ID: 0
    if (typeof(window.ysm_customData) != ‘object’) window.ysm_customData = new Object();
    window.ysm_customData.segment_1SE66WP06H0 = "event=0,transId=,currency=,amount=";
    // –>
    </SCRIPT>

    else if it’s this url, http://test.tester.local/quote, run this analytics code:

    <SCRIPT language="JavaScript" type="text/javascript">
    <!– Yahoo! Inc.
    // Event Type ID: 1
    // Segment Type ID: 0
    if (typeof(window.ysm_customData) != ‘object’) window.ysm_customData = new Object();
    window.ysm_customData.segment_1SE66WP06H0 = "event=0,transId=,currency=,amount=";
    // –>
    </SCRIPT>

    I hope this was clear. Thanks again for any help.

    # December 8, 2009 at 4:35 pm

    There are a lot of different ways of doing this. PHP_SELF would be pretty safe for this:

    Code:
    $mypath = $_SERVER[“PHP_SELF”];

    if (substr_count($mypath, “customer”) > 0) { // Catch customer

    } elseif (substr_count($mypath, “documents”) > 0){ // Catch documents

    } else { // Catch all

    }

    # December 8, 2009 at 4:48 pm

    Thank you for taking the time to respond JaredAM.

    I have one problem that I’m sure is all syntax. The (3) specific urls that I’m targeting are these:

    http://test.tester.local/customer (Home Page)
    http://test.tester.local/customer/auto/va/quote (Quote Page)
    http://test.tester.local/customer/auto/va/documents (Documents Page)

    The problem that I’m finding is that all (3) urls have at least this text:
    http://test.tester.local/customer

    So the if/else statement is negating the other pieces of analytics, and using only the one piece of analytics assigned to the home page (http://test.tester.local/customer)

    Is this an easy fix in the if/else statement?

    # December 8, 2009 at 7:19 pm

    Humm, with that tree, PHP_SELF will still work, but you’ll have to rearrange the if/elses or else customer would be caught right off the bat.

    Code:
    $mypath = $_SERVER[“PHP_SELF”];

    if (substr_count($mypath, “documents”) > 0) { // Catch documents

    } elseif (substr_count($mypath, “quote”) > 0){ // Catch quote

    } else { // Catch all (customer)

    }

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".