【发布时间】:2012-03-06 10:18:59
【问题描述】:
我正在尝试在我的 Wordpress cmets 表单上放置一个自定义事件,并希望在每次有人单击提交按钮时跟踪一个事件。我遇到的问题是表单提交并创建页面请求,而 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 表单,但我不确定让它工作的最佳方法。
感谢您的任何帮助或想法。
【问题讨论】:
-
你的例子对我有用。究竟什么不适合你?
-
好吧,我可以让表单提交,但不触发 GA 代码(因为它加载 wp-cmets-post.php 太快),或者我可以让代码提交GA 事件,但不提交表单。在上面的示例中,表单提交,但不幸的是,一旦单击提交按钮,它就会提交,这意味着 GA 事件没有时间发送自定义事件。谢谢你帮我看看。
标签: php jquery html wordpress google-analytics