You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce新用户注册时通过Session添加头部跟踪脚本的技术咨询

Got it, here's a structured, refined version of your solution to add the Facebook CompleteRegistration tracking script immediately after a new user finishes registering on your WooCommerce site:

Implementation Breakdown

1. Add Session Marker in functions.php

We'll use the user_register action hook to set a session flag when a user successfully registers. This flag lets us identify users who just completed the registration process:

add_action('user_register', 'registration_save');
function registration_save($user_id) {
    if ($user_id) {
        $_SESSION["user_registered"] = true;
    }
}

Important: If your WordPress site doesn't automatically start sessions, you'll need to add session_start(); at the very top of your functions.php file. Without this, the $_SESSION variable won't work as expected.

2. Output Tracking Script in header.php

Next, we check for the session flag in your theme's header. If it exists, we output the Facebook pixel tracking script, then immediately unset the flag to prevent the script from firing on subsequent page loads:

<?php if (isset($_SESSION["user_registered"])): ?>
    <script>fbq('track', 'CompleteRegistration');</script>
    <?php unset($_SESSION["user_registered"]); ?>
<?php endif; ?>
Quick Notes
  • Make sure you've already loaded the base Facebook pixel code on your site—this tracking script won't work without it.
  • If you're using a child theme, always modify the child theme's functions.php and header.php files. This way, your custom code won't get overwritten when the parent theme updates.

内容的提问来源于stack exchange,提问作者Elichy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:37:24