Question

How can I incorporate Google Analytics In-Page search tracking via my following search code (which is set to google search "KEYWORDS site:MYDOMAIN.com"?

The issue I face is that hitting Enter causes the user to immediately jump to google for the search query. I've seen some bits and pieces of using hitCallback but I'm unsure of how to implement this on a page of this setup.

<form action="http://www.google.com/search" name="searchbox";
method="get" id="searchform" class="form-search">;
<input type="hidden" name="hl" value="en" />;
<input type="hidden" name="ie" value="ISO-8859-1" />;
<input type="hidden" name="sitesearch" id="s" value="MYDOMAIN.com" />;
<input maxlength="256" size="40" name="q" class="search" value="" />;
</form>

I'm aware I can't use the traditional in-page search offered by the new Universal Analytics but I don't see why there shouldn't be a way to, on submit, submit to GA prior to leaving my domain for the google search page. And in thinking through things I believe one could redirect to another page, submit the GA, then redirect to google but I'd certainly like to minimize my requests and come up with a straightforward solution.

Was it helpful?

Solution 2

    <script>

    $("#searchform").submit(function (event) {



        event.preventDefault(); // cancel default behavior

        var searchTerms = $('input[name=q]').val();
        searchTerms = searchTerms.replace(" ", "+");
        console.debug(searchTerms);
        ga('send', {
            'hitType': 'pageview',
            'page': "http://MYDOMAIN.com/search?hl=en&ie=ISO-8859-1&sitesearch=MYDOMAIN.com&q=" + searchTerms,
            'title': 'Google Search'
        });
        setTimeout(function () {
            window.location.href = "http://google.com/search?hl=en&ie=ISO-8859-1&sitesearch=MYDOMAIN.com&q=" + searchTerms;
        }, 500);

    });

     </script>

The best method I found to achieving a pseudo in-site-search (delivered via google search) despite visiting an external domain was to cause a fake page view prior to redirecting to the correct search page. I inserted a half-second delay to ensure the page view was sent prior to redirect.

OTHER TIPS

You could remove the action from the form and set it with jQuery after putting a timeout on it to let the GATC fire. Something like this:

$('#searchform').on('submit', function(e){
    e.preventDefault();
    window.setTimeout(function(){
        if (typeof _gaq != 'undefined') {
            _gaq.push(['_trackEvent', 'Search', 'value', 'paramX', 'valueY', false]);
        }                                        

        $('#searchform').attr('action', 'http://www.google.com/search');

    },1000);

});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top