0

I am trying to remove special characters from Google Analytics tags. I have the following script and I can't figure out why it's not working. It's a mix of JSP and JavaScript, which I know very little about! On the job learning...

                <script type="text/javascript">
                function removeSplChars(inStr) {
                inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
                return inStr;
                }
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', '<c:out value="${profileId}"/>']);
                <c:choose>
                <c:when test="${(lastCmdName eq 'CategoryDisplay') or (lastCmdName eq 'ProductDisplay')}" >
                _gaq.push(['_setCustomVar',
                2, // This custom var is set to slot #2.
                '<c:choose><c:when test="${WCParam.source eq 'search'}">Search</c:when><c:otherwise><c:out value="${topCat}" /></c:otherwise></c:choose>', // The top-level name for your online content categories.
                '<c:choose><c:when test="${WCParam.source eq 'search'}">Search <c:out value="${WCParam.searchTerm}" /></c:when><c:otherwise><c:out value="${topCat}" />|<c:out value="${subCatA}" />|<c:out value="${subCatB}" />|<c:out value="${subCatC}" /></c:otherwise></c:choose>', // Records value of breadcrumb name
                3 // Sets the scope to page-level.
                ]); 
                </c:when>
                <c:otherwise>
                </c:otherwise>
                </c:choose>
                removeSplChars(<c:out value="${topCat}" />, <c:out value="${subCatA}" />, <c:out value="${subCatB}" />, <c:out value="${subCatC}" />);
                 _gaq.push(['_trackPageview']);
                (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();
                </script>

Am I missing some () or something somewhere? I get an "unexpected token ;" error message in the console. I tried placing the call for removeSplChars within a $(document).ready(function() {});

to no avail.

I have checked the following posts but the advice does not help me: Function is not Called javascript function not getting called

I have another post here: JavaScript Remove special characters string not working

Thank you.

Adding code as seen via View Source:

                <script type="text/javascript">
                function removeSplChars(inStr) {
                inStr = inStr.replace(/[^a-zA-Z0-9 ]/g, "");
                return inStr;
                }       
                var _gaq = _gaq || [];
                _gaq.push(['_setAccount', 'UA-33021136-1']);


                _gaq.push(['_setCustomVar',
                2, // This custom var is set to slot #2.
                'Dine &amp; Entertain', // The top-level name for your online content categories.
                'Dine &amp; Entertain|Bowls &amp; Platters||', // Records value of breadcrumb name
                3 // Sets the scope to page-level.
                ]); 



                $(document).ready(function() {
                removeSplChars(Dine &amp; Entertain, Bowls &amp; Platters, , );
                });
                _gaq.push(['_trackPageview']);

                (function() {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
                })();

                </script>
Community
  • 1
  • 1
surfbird0713
  • 1,209
  • 2
  • 23
  • 45
  • 3
    It is always better to look at the generated code in the browser [aka view source] and not the serverside markup. – epascarello Oct 05 '12 at 15:32
  • @epascarello, thank you, I have updated my post to show that code. I would be so grateful for any help you can offer. – surfbird0713 Oct 05 '12 at 15:56

1 Answers1

0

Your strings passed to removeSplChars are not enclosed in quotes, so you get errors. It's more reliable to pass variables after JSON-encoding them if you can. Alternatively, you could just put " around it and hope for the best (if you already encoded " as &quot; in the input, it should work fine).

Additionally, this will take care of the issue you're having with empty arguments: empty strings will be passed as arguments instead.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592