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 开发者_如何学Gosimple 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?
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>"
.
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.
You just have to call your javascript code from the onSubmit argument of the form.
<form name="..." action="..." onSubmit="_gaq.push(['_trackPageview', 'virtual/formSubmit']);">
echo " <script type='text/javascript'>_gaq.push(['_trackPageview', 'virtual/formSubmit']); </script> ";
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>';?>;
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.).
精彩评论