Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm trying to get an event to be tracked (user registration) as a goal. I DON'T want it to be tracked by an onclick, I want it to be tracked once a PHP event is finished. Here is my code:

if(USER REGISTRATION SUCCESSFUL):
<script type="text/javascript">
   var _gaq = _gaq || [];
   _gaq.push(['_trackEvent', 'SignUp', 'Registered', 'User Registered']);
</script>
endif;

The regular Google Analytics Code is in the footer just before the / body. On the register page that code is being triggered in the middle of the page.

But the completed goals aren't showing up in Google Analytics, any idea why?

Note: PHP simplified since it doesn't really pertain to this question.

share|improve this question
    
Does your event code come before or after the regular GA code? –  Ewan Heming May 1 '12 at 14:52
    
It comes before, does it have to come after? The way the PHP document is structured it would be very difficult to make the code come after –  Talon May 1 '12 at 21:05

1 Answer 1

up vote 3 down vote accepted

Try removing var _gaq = _gaq || []; - this line is obliterating your existing Google Analytics object and Events cannot be tracked without a call to trackPageView.

Edit: PHP Code Sample

Sorry, I missed the fact that your GA.js snippet appears after your Event tracking snippet - here is a solution which should resolve the issue:

<?php // start of document
if ( USER REGISTRATION SUCCESSFUL )
{
    $analytics_addition = "_gaq.push(['_trackEvent', 'SignUp', 'Registered', 'User Registered']);";
}
?>

<!--
      your regular HTML output
-->

<!-- ga.js snippet -->
<script type="text/javascript">
    var _gaq = _gaq || [];
    _gaq.push(['_setAccount', 'UA-XXXXX-X']);
    _gaq.push(['_trackPageview']);
<?php
    // append event tracking 
    if ( @isset($analytics_addition) && $analytics_addition )
      echo $analytics_addition;
?>
    (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>
<!-- eof ga.js snippet -->
share|improve this answer
    
I tried it without that code at the top before I tried this and it still didn't work. The _gaq.push(['_trackPageview']); is at the bottom of the HTML document. –  Talon May 1 '12 at 21:03
    
Ah, then your Google Analytics object doesn't exist yet at all - in which case the GA.js snippet's variable declaration is obliterating the _gaq object (which is probably throwing a script error) - your best bet would be to write the Event push to a PHP variable and echo the PHP variable within your GA.js snippet. (added example) –  danlefree May 1 '12 at 22:53
    
Good idea, I did something similar, I set a variable to true and then at the bottom of the analytics page I added the script if the variable was true, but your method is more readable so I think i'll do that in the future. –  Talon May 2 '12 at 2:37

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.