ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Setting up Google Analytics "Site Search"

December 13, 2007 4:18pm

Subscribe [9]
  • #1 / Dec 13, 2007 4:18pm

    asozzi

    262 posts

    Has anyone set up EE to use the Google Analytics Site Search functionality?

    What would I need to put for the ‘Query Parameter’? The search box ends up as the following code on the page:

    <form action="http://www.ehs.sfu.ca/" method="post">
    <div class="hiddenFields">
    <input type="hidden" value="19" name="ACT"/>
    <input type="hidden" value="" name="XID"/>
    <input type="hidden" value="search/results" name="RP"/>
    <input type="hidden" value="search/noresults" name="NRP"/>
    <input type="hidden" value="" name="RES"/>
    <input type="hidden" value="not home" name="status"/>
    <input type="hidden" value="not ehsblog" name="weblog"/>
    <input type="hidden" value="everywhere" name="search_in"/>
    <input type="hidden" value="all" name="where"/>
    <input type="hidden" value="1" name="site_id"/>
    </div>
    <label class="inside" for="keywords">Search EHS…</label>
    <input id="keywords" type="text" maxlength="100" name="keywords" value=""/>
    <input id="top-submit" type="image" src="/images/search4.4.gif" alt="Submit"/>
    </form>

    so the search terms would be: “search/results” ?

  • #2 / Dec 16, 2007 1:56am

    asozzi

    262 posts

    Found the solution:

    Google just announced an updated GAnalytics code that allows some neat stuff:
    - Track Searches (even page internal searches)
    - Track Events (i.e file downloads)

    1) Set up a GAnalytics account for your site (follow instructions) USE the NEW ga.js code!
    Or use this PDF Guide on how to move from the old Urchin code to the new stuff

    I added the tracking code in a (EE) Global Variable in the footer part of my templates.

    2) Activate the Site Search function as described here. Or follow this even better blog post on how to do so.


    The problem starts here since EE does not submit it searches as URL encoded strings but in a POST’ed form. So the entire part after the “Query Parameter” becomes moot

    Solution:
    1)  Set up the GA “Query Parameter” to anything you want. I used the google like “q”. So just enter a single “q” and save.

    2) Enter your EE-CP and go to your “Search Results” template (most likely search/results). On this page we change the GAnalytics code a bit, so replace the Global Variable (if you used one) with the actual code

    Now instead of tracking the page using the standard

    pageTracker._trackPageview();

    call, we call a hit on a “virtual” URL (i.e we make up an URL and call a page view). To do so exchange the standard call with:

    pageTracker._trackPageview('/search?q={exp:search:keywords}');

    If someone searched for: “best CMS”
    GA now recorded a hit for the “virtual” page: “www.yourdomain.com/search?q=best CMS”

    And since we told GA to look out for search term after the Query Parameter: “q”. The search is tracked.

    Minor cleanup Issues:
    - GA tracks cmS, CMS or Cms as different searches.
    - GA converts spaces to %20; but turns + into a space.

    solution:

    <pre><code>terms=’{exp:search:keywords}’;
    searchterms=terms.toLowerCase().replace(/\s/g,’+’);
    pageTracker._trackPageview(’/search?q=’+searchterms);</code></pre>

    convert all search terms to lower case and replace spaces with + before submitting.


    Hope it helps someone. I’ll tackle the File Download tracking tomorrow.

  • #3 / Jan 05, 2008 9:53pm

    ronin

    8 posts

    Thanks for tackling this. Trying it out to see if it works!

  • #4 / Jan 11, 2008 2:56pm

    asozzi

    262 posts

    Hope you were successful,it works a treat for me.

    I added it to the wiki now.

  • #5 / Jul 17, 2008 3:51pm

    keystone

    21 posts

    Very helpful, thanks asozzi. If anyone is like me and puts their analytics code in an embedded footer template you can use conditionals to put the special parameters in for search results.

    {if segment_2 == "results"} {!-- Special tracking code for search results page --}
        [removed]
        var pageTracker = _gat._getTracker("---");
        pageTracker._initData();
        terms='{exp:search:keywords}';
        searchterms=terms.toLowerCase().replace(/\s/g,'+');
        pageTracker._trackPageview('/search?q='+searchterms);
        [removed]
    {if:else}
        [removed]
        var pageTracker = _gat._getTracker("---");
        pageTracker._initData();
        pageTracker._trackPageview();
        [removed]
    {/if}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases