Jump to content

If/Else statement, different analytics code depending on URL


Recommended Posts

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.

$requested = $_SERVER["REQUEST_URI"];

if (stristr($requested, "query") !== false) {

}elseif (stristr($requested, "documents") !== false) {

}else {

}

Would be one way to do it.

True, but the only problem with this is that if the url contains the keyword 'documents', then it would return the first. For example:

 

http://test.tester.local/customer/documents

 

Then it would return the first one. You would have to check the entire url. I would suggest:

 

$requested = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(stristr($requested, "http://".$_SERVER['HTTP_HOST']."/query") !== false) {
} elseif (stristr($requested, "http://".$_SERVER['HTTP_HOST']."/documents") !== false) {
} else {
}

Thank you both, premiso and mattal999. I should have mentioned that I'm pretty much a novice when it comes to PHP (forte in CSS/HTML).

 

Question.... in the code that you both provided, where did "query" come from? It's got me all sorts of confused. :confused:

 

If you wouldn't mind maybe commenting each line of code for me to show what's happening/ or what should happen, that would help me learn it the best.

//this gets the current URI That was used to access the page
//for example, on www.example.com/page.php?id=stuff
//the request uri would be /page.php
$requested = $_SERVER["REQUEST_URI"];

//stristr returns if a string (the variable on the right side) is 
//inside another string (left side)
if (stristr($requested, "query") !== false) {
//if this is true, then the word "query" is inside the request URI
}elseif (stristr($requested, "documents") !== false) {

}else {

}

 

Thanks to all of you for your help. I'm a little smarter, and a little more informed. i feel like i'm one step closer to completing the code. 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

http://test.tester.local/customer/auto/va/quote

http://test.tester.local/customer/auto/va/documents

 

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

 

Here's what I have

<?
$requested = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if(stristr($requested, "http://".$_SERVER['HTTP_HOST']."") !== false) { 
?>
<!—LOGIN PAGE, YAHOO PROSPECT START TAG -->
<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_1SE65A0 = "event=0,transId=,currency=,amount=";
// -->
</SCRIPT>
<!—LOGIN PAGE, YAHOO PROSPECT END TAG -->

<? } elseif (stristr($requested, "http://".$_SERVER['HTTP_HOST']."/auto/va/quote") !== false) { ?>
<!— QUOTE PAGE,  YAHOO CONVERSION TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 1
// Segment Type ID: 1
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_TSP06H0 = "event=1,transId=,currency=,amount=";
// -->
</SCRIPT>
<!— QUOTE PAGE,  YAHOO CONVERSION END TAG -->

<!—QUOTE PAGE,  YAHOO PROSPECT START TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 2
// Segment Type ID: 0
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_1FR956S = "event=0,transId=,currency=,amount=";
// -->
</SCRIPT>
<!—QUOTE PAGE,  YAHOO PROSPECT END TAG -->

<? } elseif (stristr($requested, "http://".$_SERVER['HTTP_HOST']."/auto/va/forms") !== false) { ?>
<!—POLICY DOCUMENTS  YAHOO CONVERSION START TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 2
// Segment Type ID: 1
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_1FR95688BNU0CS = "event=1,transId=,currency=,amount=";
// -->
</SCRIPT>
<!— POLICY DOCUMENTS ,  YAHOO CONVERSION END TAG -->

<? } else { ?>
<? } ?>

 

Is it something that's amazingly easy?

Yes. You need to move the first check to be the last. It needs to see if any other pages are being requested before assuming the root.

 

<?php
$requested = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
if (stristr($requested, "http://".$_SERVER['HTTP_HOST']."/auto/va/quote") !== false) { ?>
<!— QUOTE PAGE,  YAHOO CONVERSION TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 1
// Segment Type ID: 1
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_TSP06H0 = "event=1,transId=,currency=,amount=";
// -->
</SCRIPT>
<!— QUOTE PAGE,  YAHOO CONVERSION END TAG -->

<!—QUOTE PAGE,  YAHOO PROSPECT START TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 2
// Segment Type ID: 0
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_1FR956S = "event=0,transId=,currency=,amount=";
// -->
</SCRIPT>
<!—QUOTE PAGE,  YAHOO PROSPECT END TAG -->

<?php } elseif (stristr($requested, "http://".$_SERVER['HTTP_HOST']."/auto/va/forms") !== false) { ?>
<!—POLICY DOCUMENTS  YAHOO CONVERSION START TAG -->
<SCRIPT language="JavaScript" type="text/javascript">
<!-- Yahoo! Inc.
// Event Type ID: 2
// Segment Type ID: 1
if (typeof(window.ysm_customData) != 'object') window.ysm_customData = new Object();
window.ysm_customData.segment_1FR95688BNU0CS = "event=1,transId=,currency=,amount=";
// -->
</SCRIPT>
<!— POLICY DOCUMENTS ,  YAHOO CONVERSION END TAG -->

<?php } elseif(stristr($requested, "http://".$_SERVER['HTTP_HOST']."/") !== false) { ?>
<!—LOGIN PAGE, YAHOO PROSPECT START TAG -->
<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_1SE65A0 = "event=0,transId=,currency=,amount=";
// -->
</SCRIPT>
<!—LOGIN PAGE, YAHOO PROSPECT END TAG -->

<?php } ?>

 

Oh, and always use the proper tags (<?php), not shorttags (<?) when starting PHP.

Thanks Mattal999. I did try that solution of rearranging the elseif statements so that the "customer" page would run last, but for some reason, the three URLS still only show the analytics that's supposed to only be on the "customer" page.

 

When I take out the last elseif statement that runs the analytics for the root and only leave the "quote" and "forms" code, no analytics show up in the source code at all.

 

I've been trying to read up on the code that you helped write, and have a question....

 

Does the fact that this site is a subdomain of another site have anything to do with it?

I was checking on the definitions of $_SERVER and HTTP_HOST.

 

We have a regular website: http://www.domain.com and we have this site which needs the analytics, which is a subdomain: http://subdomain.domain.com/customer. Does this have anything to do with why the php code is not working?

 

I read something about subdomains and  SERVER_NAME , but don't know if i'm on the right track.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.