I am brand new to JavaScript and JSP coding, and have been tasked with something that feels over my head - hoping someone here can help me out.
The goal of my project is to dynamically populate fields to pull in the breadcrumb trail from a page into a custom variable in Google Analytics. What I need to do is populate the fields:
_gaq.push(['_setCustomVar',
2,
'BreadCrumb2',
'BreadCrumb3 | Breadcrumb 4 | Breadcrumb 5',
3
]);
with information from our breadcrumb trail that is set up like this (sometimes it only goes as deep as breadcrumb 3 but there could be up to 2 additional):
Home >> BreadCrumb2 >> BreadCrumb3 >> BreadCrumb4
I have played around with the existing code that sets up our breadcrumbs to no avail - Google Debugger throws an error whenever I have any versions of this code in there:
Code:
<c:set var="breadCrumbs" value="${categoryVB.breadCrumbs}" />
<c:set var="topCategory" value="${breadCrumbs}" />
<c:set var="subCategory" value="${breadCrumbs}" />
<c:forEach var="breadCrumb" items="${breadCrumbs}" varStatus="status" >
</c:forEach>
<c:choose>
<c:when test="${lastCmdName eq 'CategoryDisplay'}">
<script type="text/javascript">
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #2.
'<c:choose><c:when test="${status.index != 2}">
<c:out value="${topCategory.value}" />
</c:when><c:otherwise>
</c:otherwise></c:choose>', // I am trying to tell it to pull in the information from the 2nd breadcrumb in the path
'<c:if test="${status.index != 2}" >
<c:choose>
<c:when test="${status.last}">
<c:out value="${subCategory.value}" />
</c:when>
<c:otherwise>
<c:out value="${topCategory.value}" />
</c:otherwise>
</c:choose>
</c:if>', // Trying to tell it to pull in the values after the 2nd breadcrumb if they are there, otherwise, fill this spot with the value of the 2nd breadcrumb.
3 // Sets the scope to page-level.
]);
</script>
</c:when>
<c:otherwise>
</c:otherwise>
</c:choose>
I need to figure out how to manipulate that code, or is there a way I can use JavaScript to create a variable for me based on the information? So I can say:
JavaScript:
Tell me what it says between the first instance of >> and second instance of >> and create something called "Category"
and then I can put that into the Google code?
_gaq.push(['_setCustomVar',
2,
'JavaScriptVariableCategory',
'Everything After BreadCrumb3',
3 // Sets the scope to page-level.
]);
I would appreciate any and all advice, hints, help anyone can offer. I feel very lost here.
Thanks