Search code examples
jsongoogle-analyticsgoogle-custom-search

CSE JSON API and Google Analytics


We post our queries directly to the Google CSE JSON Api, as such we do an ajax call that looks something like this:

https://www.googleapis.com/customsearch/v1?q=[query]&cx=[cx]&hq=[filters]&key=[key]

We've setup our CSE with the correct Analytics account and put the old analytics js stuff on our page:

<script>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'number']);
_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);
})();

and configured the CSE to use the query q. In Google analytics we have site search tracking on and our query parameter is set to q. The CSE stats show a decent amount of distinct queries every day but we get nothing in Google Analytics.

When posting directly to the JSON API is there something additional I need to do to get the analytics account hooked up to show queries in the site search reports?


Solution

  • I fixed this by switching to universal analytics and sending GA a fake page with query:

    ga('send', 'pageview', '/search-results.php?q=' + ((query) ? query : Search.searchViewModel.query()) );