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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Julien
  • 11
  • 1
  • Those other SO Q&A may help you: http://stackoverflow.com/questions/391306/javascript-function-from-php http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – Maxime Pacary Apr 12 '11 at 20:01
  • 1
    Javascript executes on the client, PHP executes on the server. Javascript cannot call PHP code directly, and PHP can only generate Javascript code, not execute it. – Marc B Apr 12 '11 at 20:02

6 Answers6

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>".

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
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.

Jakub
  • 20,418
  • 8
  • 65
  • 92
  • 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! – Julien Apr 12 '11 at 20:21
  • Please post your questions in your QUESTION, not in comments, code is hard to read here. – Jakub Apr 12 '11 at 20:22
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']);">
Charles Brunet
  • 21,797
  • 24
  • 83
  • 124
  • 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 – CrayonViolent Apr 12 '11 at 20:07
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>';?>;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
fvox
  • 1,077
  • 6
  • 8
  • careful using short tags, I would actually advise against them, easy to make mistakes, and not supported by default. – Jakub Apr 12 '11 at 20:23
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.).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
CrayonViolent
  • 32,111
  • 5
  • 56
  • 79
0
echo " <script type='text/javascript'>_gaq.push(['_trackPageview', 'virtual/formSubmit']); </script> ";
Robin Maben
  • 22,194
  • 16
  • 64
  • 99