Pull Folders From The URL To Populate Channel and Hierarchy From Inside The S_Code File

EDIT: I have a better way of doing this. Please check out the post New Plugin Function to Populate Hierarchy From URL in Omniture SiteCatalyst. You can still glance at what I have here, but that new post is really much better.

In many organizations, trying to get the development team to implement any enhancements to your SiteCatalyst implementation is not always going to be the top thing on their to-do list. After dealing with trying to work around their schedule, and working with smaller sites that don’t have the best resources, I have come up with quite a few tricks to get as much done in the s_code file as I can. Here is a neat trick to grab folders from the directory structure to fill them in to the Site Sections or any prop or evar you would like. Check out this piece of code:

var folders = new Array();
var path = window.location.pathname;
	
path = path.replace("http://", "");
path = path.slice(path.indexOf('/') + 1, path.lastIndexOf('/'));
	
folders = path.split('/');
	for (var i = 0; i < folders.length; ++i) {
		if (folders[0] != ""){s.channel =folders[0]};
		}

What this does is takes the first folder from the URL, and plugs it right into the s.channel variable. It will not take the first file, just the folder. For example looking at the URL:
http://webanalyticsland.com/channel-name/folder2/folder3/folder4/index.php
We would end up with s.channel=channel-name
Most sites ‘should’ have a good directory structure:
Ideal Directory Structure
so that first folder can usually be considered a channel of the site.

EDIT: Again, I would ignore this following section. There are some errors with my logic, and I can admit when I made a mistake. I really do recommend you check out the new post I have shown above.

If your site is laid out a little differently, another neat use of this code, is if you want to use it to populate the Hierarchy variable (s.heirN). By making a few changes to the code, you can set the heir1-heir5 variables right in the script:

var folders = new Array();
var path = window.location.pathname;
	
path = path.replace("http://", "");
path = path.slice(path.indexOf('/') + 1, path.lastIndexOf('/'));
	
folders = path.split('/');
	for (var i = 0; i < folders.length; ++i) {
		if (folders[0] != ""){s.heir2 =folders[0]};
		if (folders[1] != ""){s.heir3 =folders[1]};
		if (folders[2] != ""){s.heir4 =folders[2]};
		if (folders[3] != ""){s.heir5 =folders[3]};
		}
s.hier1="home";     

This assigns the value of ‘home’ to any page with no folders, basically your home page any any files directly attached to it, and counts each folder after that as one more step in the hierarchy. Looking again at that example URL:
http://webanalyticsland.com/channel-name/folder2/folder3/folder4/index.php
The code would populate the variables like:
s.hier1=channel-name
s.hier2=folder2
s.hier3=folder3
s.hier4=folder4

Give it a try. Any time you can save your developers is just a bonus for you. It is so much easier to get a new s_code file pushed live, than to get coding done on the site.

Leave a Reply