1

I have a simple PHP function that is being called when a form is submitted:

function uc_paypal_ec_submit_form($form, &$form_state) {
  $form['actions'] = array('#type' => 'actions');
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit order'),
  );
  return $form;
}

What I need is to be able to do a simple Google Analytics call like this:

_gaq.push(['_trackPageview', 'virtual/formSubmit']);

I've tried a few options, but nothing works. I don't see the call being made to Google Analytics...

How can I fix this problem?

2

6 Answers 6

1

Do you know that JavaScript is running in the client's browser while PHP is running on the server?

Anyway, now you do. So that should answer your question.

Simply echo <script type="text/javascript">yourJsCodeHere</script>".

Sign up to request clarification or add additional context in comments.

Comments

0

Make your function return the output and have the output print in the GOOGLE code <script> element, will make the _gaq.push... fire off.

You can't just output that JS and expect it to fire by itself somewhere... or for PHP to make a call to Google.

  • PHP is SERVER SIDE
  • JS is CLIENT SIDE (clients browser requests from google, not your server)

So like I said, make the JS output to a function that fires onload and you are all set. save the output: $output = "_gaq.push(['_trackPageview', 'virtual/formSubmit']);";

and then echo it somewhere to fire it.

2 Comments

Hey Jakub, seems promising, would you care to elaborate? So if I use the same function as in my example, can I use this line ($output = "_gaq.push(['_trackPageview', 'virtual/formSubmit']);";) at the beginning of the function and then "echo = $output" at the end? Thanks a lot!
Please post your questions in your QUESTION, not in comments, code is hard to read here.
0

You just have to call your javascript code from the onSubmit argument of the form.

<form name="..." action="..." onSubmit="_gaq.push(['_trackPageview', 'virtual/formSubmit']);">

1 Comment

this isn't really a good idea. For one thing, at a minimum you will want to at least timeout the submit to give GA a chance to execute it. But more importantly, this will give false positives. For example, user submits and server-side form validation fails and user is kicked back..well this gives a false positive that the user completed the form when they did not (or even if they validate client-side). IOW it needs to be triggered after validation is successful to be accurate
0
echo " <script type='text/javascript'>_gaq.push(['_trackPageview', 'virtual/formSubmit']); </script> ";

Comments

0

You can't. JavaScript is client side, and PHP is server side.

You can make the browser call a JavaScript function:

<?='<script type="text/javascript">jsfunc();<;/script>';?>;

1 Comment

careful using short tags, I would actually advise against them, easy to make mistakes, and not supported by default.
0

The short and easy answer is you can't execute JavaScript code server-side. The long and hard answer is you can, but it's long and hard and involves some server-side scripts/libraries and is kind of messy at best.

Basically, what you want to do is either output that code on the confirmation/thank you page the user lands on after (optimal) or else use cURL to make a hit (it is a little trickier... Basically you have to simulate the img request, appending the relevant information, including cookie information... Note, this is not the "long and hard" method mentioned above.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.