我正在尝试在我的 WordPress 评论表单上放置一个自定义事件,并且希望在每次有人单击提交按钮时跟踪该事件。我遇到的问题是表单提交并创建页面请求,而 Google 分析代码没有时间工作。
我需要延迟表单提交,以便跟踪有时间进行。我查找了类似的问题并尝试了各种解决方案,但似乎无法让它们发挥作用。任何帮助将不胜感激。
我的代码如下,我尝试延迟表单但未成功。
<form id="commentsForm" action="<?php echo get_option('siteurl'); ?>/wp-comments- post.php" method="post" id="commentform">
<!-- Delay form submit to allow GA code to send -->
<script type="text/javascript">
$(document).ready(function(){
$('#commentsForm').submit(function (e) {
var form = this;
e.preventDefault();
setTimeout(function () {
form.submit();
}, 1000); // in milliseconds
});
});
</script>
<?php if ( is_user_logged_in() ) : ?>
<p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><? php echo $user_identity; ?></a>. <a href="<?php echo wp_logout_url(get_permalink()); ?>" title="Log out of this account">Log out »</a></p>
<?php else : ?>
<p class="textfield">
<input name="author" id="author" value="<?php echo esc_attr($comment_author); ?>" size="22" tabindex="1" type="text" <?php if ($req) echo "aria-required='true'"; ?>>
<label for="author"><small>Name <?php if ($req) echo "(required)"; ?></small></label> </p>
<p class="textfield"><input type="text" name="email" id="email" value="<?php echo esc_attr($comment_author_email); ?>" size="22" tabindex="2" <?php if ($req) echo "aria-required='true'"; ?>>
<label for="email"><small>Mail (will not be published) <?php if ($req) echo "(required)"; ?></small></label></p>
<p class="textfield"><input type="text" name="url" id="url" value="<?php echo esc_attr($comment_author_url); ?>" size="22" tabindex="3">
<label for="url"><small>Website</small></label></p>
<?php endif; ?>
<!--<p><small><strong>XHTML:</strong> You can use these tags: <code><?php echo allowed_tags(); ?></code></small></p>-->
<p class="text-area"><textarea name="comment" id="comment" cols="58" rows="10" tabindex="4"></textarea></p>
//Submit button
<p><input class="submitButton" onClick="_gaq.push(['_trackEvent','Comment','Submit', URL]);" name="submit" type="submit" id="submit" tabindex="5" value="" />
<?php comment_id_fields(); ?>
</p>
<?php do_action('comment_form', $post->ID); ?>
</form>
我认为它可能不起作用,因为该表单是 PHP 表单,但我不确定让它工作的最佳方法。
感谢您的任何帮助或想法。
最佳答案
您可能遇到了提交 onclick
触发和 form.submit()
事件触发的顺序问题。您可以尝试将点击事件从提交按钮中拉出来,然后将其放入提交函数中吗?
类似这样的事情:
$(document).ready(function(){
$('#commentsForm').submit(function (e) {
var form = this;
e.preventDefault();
_gaq.push(['_trackEvent','Comment','Submit', URL]);
setTimeout(function () {
form.submit();
}, 1000); // in milliseconds
});
});
另外,也许我在你的例子中错过了它,但是URL
是什么?
关于php - 使用 jQuery 延迟 WordPress 评论表单发布,以允许分析发送跟踪。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9570611/