Currently, I am working on a website that utilizes Ajax calls to update the main content. In order to integrate Google Analytics tracking code using the async _gaq method, I need to push a _trackPageview event with the URI to _gaq. There are two approaches I am considering:
1) Utilize an onclick property (or utilize JQuery to bind events) on links and forms to include the _gaq.push(....)
2) Given that my ajax code can process scripts upon receiving data from the ajax request, I could incorporate the _gaq.push(...) call within a script snippet when serving the ajaxed page.
The first option results in cleaner and more concise code but requires remembering to add the analytics code to all links and forms. Additionally, if used on a form with JS validation, the click will be tracked by GA even if validation fails.
The second option may seem less clean, but it resolves the aforementioned issues. It allows for a single GA code to determine whether we're serving ajax or normal pages and output a suitable JS snippet accordingly. Moreover, form submissions will only be logged when a form is successfully submitted.
I welcome any thoughts or feedback on this matter. Are there additional considerations or alternative methods that should be taken into account?
Thank you, Yaron