开发者

PHP variable inside Google Analytics Code

I have the following code

if ($form_id == 1)
{

    $form_name = "Test form 1";
}
elseif ($form_id == 2)
{
    $form_name = "General Enquiries";
}
else
{
    // do something
}

I now need to echo $form_name within this bit of code but cant seem to be able to get it t开发者_高级运维o show.

<form id="form_7" class="appnitro" action="#main_body" method="post" onSubmit="_gaq.push(['_trackPageview', '/virtual/general-enquiries']);">

The bit that says /virtual/general-enquiries needs to be replace by the php echo.

Thanks Roy


It does need to be echoed by PHP. The server-side code will be interpreted long before Google Analytics code gets interpreted on the client. Something like this should work for you:

<?php
if ($form_id == 1)
{

    $form_name = "Test form 1";
}
elseif ($form_id == 2)
{
    $form_name = "General Enquiries";
}
else
{
    // do something
}

if(isset($form_name))
{
    echo '<form id="' . $form_id . '" class="appnitro" action="#main_body" method="post" onSubmit="_gaq.push([\'_trackPageview\', \'' . $form_name . '\']);">';
}


This will do it:

"...onSubmit="_gaq.push(['_trackPageview', '<? echo $form_name; ?>']);"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜