我想在我的Wordpress评论表单上放置一个自定义事件,并希望每次有人点击提交按钮时跟踪一个事件。我遇到的问题是表单提交并创建了页面请求,Google分析代码没有时间工作。使用jQuery延迟WordPress的评论形式发布以允许分析发送跟踪。
我需要延迟表单提交,以便跟踪有时间工作。我查了类似的问题,并尝试了各种解决方案,但似乎无法让他们工作。任何帮助将不胜感激。
我的代码如下,我试图延迟表单失败。
<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的形式,但我不知道得到它的工作的最佳途径。
感谢您的任何帮助或想法。
你的例子适合我。什么不适合你? – Basti 2012-03-05 17:04:50
嗯,我可以让表单提交,但不会触发GA代码(因为它太快地加载wp-comments-post.php),或者我可以得到它,以便代码提交GA事件但不提交表格。在上面的示例中,表单提交,但是一旦遗憾地点击了提交按钮,就意味着GA事件没有时间来定制自定义事件。感谢您为我查看它。 – Simon 2012-03-05 21:01:22