文章目录
-
- 在当今数字化营销时代,网站不仅仅是企业的线上名片,更是潜在客户转化的重要渠道。WordPress作为全球最受欢迎的内容管理系统,其强大的可扩展性为开发者提供了无限可能。通过自定义插件开发,我们可以将WordPress从一个简单的博客平台转变为功能强大的营销自动化工具。 本教程将深入探讨如何通过WordPress插件开发,实现网站自动化营销漏斗与客户旅程追踪功能。我们将从基础概念讲起,逐步构建一个完整的营销自动化插件,帮助您更好地理解客户行为,优化转化路径,并最终提升业务成果。
-
- 营销漏斗是描述潜在客户从认知到购买决策过程的模型。传统营销漏斗通常包括以下阶段: 认知阶段:用户首次接触品牌 兴趣阶段:用户对产品或服务产生兴趣 考虑阶段:用户评估不同选项 意向阶段:用户表现出购买意愿 购买阶段:用户完成交易 忠诚阶段:用户成为忠实客户并推荐他人
- 客户旅程追踪是指记录和分析用户与品牌互动的全过程。通过追踪客户旅程,企业可以: 识别转化路径中的瓶颈 个性化用户体验 优化营销策略 提高客户留存率 增加客户生命周期价值
- WordPress作为开发平台具有以下优势: 庞大的用户基础和成熟的生态系统 灵活的插件架构和丰富的API 开源特性,允许深度定制 与各种第三方服务的良好兼容性 强大的社区支持和丰富的学习资源
-
- 在开始开发之前,我们需要准备以下环境: 本地开发环境:推荐使用XAMPP、MAMP或Local by Flywheel 代码编辑器:VS Code、PHPStorm或Sublime Text WordPress安装:最新版本的WordPress 调试工具:安装Query Monitor、Debug Bar等调试插件
- 每个WordPress插件至少需要一个主文件,通常以插件名称命名。以下是创建基础插件结构的步骤: 在wp-content/plugins/目录下创建新文件夹,命名为marketing-automation-funnel 在该文件夹中创建主文件marketing-automation-funnel.php 添加插件头部信息: <?php /** * Plugin Name: 营销自动化漏斗与客户旅程追踪 * Plugin URI: https://yourwebsite.com/marketing-automation-funnel * Description: 实现网站自动化营销漏斗与客户旅程追踪功能 * Version: 1.0.0 * Author: 您的姓名 * Author URI: https://yourwebsite.com * License: GPL v2 or later * Text Domain: marketing-automation-funnel */
- 了解WordPress插件的基本架构是开发的关键: 钩子系统:动作钩子(Actions)和过滤器钩子(Filters) 短代码系统:允许在文章和页面中嵌入动态内容 自定义数据库表:存储插件特定数据 管理界面:为插件创建设置页面 AJAX处理:实现前端与后端的异步通信
-
- 客户旅程数据需要专门的数据库表来存储。我们将创建两个主要表: 客户表:存储客户基本信息 互动事件表:记录客户的每一次互动 以下是创建数据库表的代码示例: // 在插件激活时创建数据库表 register_activation_hook(__FILE__, 'maf_create_tables'); function maf_create_tables() { global $wpdb; $charset_collate = $wpdb->get_charset_collate(); // 客户表 $table_name_customers = $wpdb->prefix . 'maf_customers'; $sql_customers = "CREATE TABLE IF NOT EXISTS $table_name_customers ( id INT(11) NOT NULL AUTO_INCREMENT, email VARCHAR(100) NOT NULL, first_name VARCHAR(50), last_name VARCHAR(50), created_at DATETIME DEFAULT CURRENT_TIMESTAMP, last_seen DATETIME, funnel_stage VARCHAR(50) DEFAULT 'awareness', PRIMARY KEY (id), UNIQUE KEY email (email) ) $charset_collate;"; // 互动事件表 $table_name_events = $wpdb->prefix . 'maf_events'; $sql_events = "CREATE TABLE IF NOT EXISTS $table_name_events ( id INT(11) NOT NULL AUTO_INCREMENT, customer_id INT(11) NOT NULL, event_type VARCHAR(50) NOT NULL, event_data TEXT, page_url VARCHAR(500), referrer VARCHAR(500), created_at DATETIME DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY customer_id (customer_id), KEY event_type (event_type) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql_customers); dbDelta($sql_events); }
- 客户识别是旅程追踪的基础。我们将通过以下方式识别客户: Cookie追踪:为匿名用户设置唯一标识符 用户登录识别:对已登录用户直接识别 电子邮件识别:通过表单提交获取电子邮件 // 设置客户Cookie function maf_set_customer_cookie() { if (!isset($_COOKIE['maf_customer_id'])) { $customer_id = uniqid('maf_', true); setcookie('maf_customer_id', $customer_id, time() + (365 * 24 * 60 * 60), '/'); } } add_action('init', 'maf_set_customer_cookie'); // 识别当前客户 function maf_identify_customer() { global $wpdb; $customer_id = null; // 检查是否已登录 if (is_user_logged_in()) { $user_id = get_current_user_id(); $user_info = get_userdata($user_id); // 检查用户是否已存在于客户表中 $table_name = $wpdb->prefix . 'maf_customers'; $existing_customer = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM $table_name WHERE email = %s", $user_info->user_email ) ); if ($existing_customer) { $customer_id = $existing_customer->id; } else { // 创建新客户记录 $wpdb->insert( $table_name, array( 'email' => $user_info->user_email, 'first_name' => $user_info->first_name, 'last_name' => $user_info->last_name, 'created_at' => current_time('mysql') ) ); $customer_id = $wpdb->insert_id; } } // 检查Cookie elseif (isset($_COOKIE['maf_customer_id'])) { $cookie_id = sanitize_text_field($_COOKIE['maf_customer_id']); // 查找基于Cookie的客户记录 $table_name = $wpdb->prefix . 'maf_customers'; $existing_customer = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM $table_name WHERE email = %s", $cookie_id . '@anonymous.com' ) ); if ($existing_customer) { $customer_id = $existing_customer->id; } } return $customer_id; }
- 客户与网站的每一次互动都应该被记录: // 记录客户事件 function maf_track_event($event_type, $event_data = array()) { global $wpdb; $customer_id = maf_identify_customer(); if (!$customer_id) { // 为匿名用户创建临时记录 $table_name = $wpdb->prefix . 'maf_customers'; $cookie_id = isset($_COOKIE['maf_customer_id']) ? sanitize_text_field($_COOKIE['maf_customer_id']) : uniqid('maf_', true); $wpdb->insert( $table_name, array( 'email' => $cookie_id . '@anonymous.com', 'created_at' => current_time('mysql'), 'last_seen' => current_time('mysql') ) ); $customer_id = $wpdb->insert_id; } // 更新客户最后访问时间 $wpdb->update( $wpdb->prefix . 'maf_customers', array('last_seen' => current_time('mysql')), array('id' => $customer_id) ); // 记录事件 $table_name = $wpdb->prefix . 'maf_events'; $wpdb->insert( $table_name, array( 'customer_id' => $customer_id, 'event_type' => sanitize_text_field($event_type), 'event_data' => maybe_serialize($event_data), 'page_url' => isset($_SERVER['REQUEST_URI']) ? esc_url_raw($_SERVER['REQUEST_URI']) : '', 'referrer' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : '', 'created_at' => current_time('mysql') ) ); return $wpdb->insert_id; } // 自动追踪页面访问 function maf_track_page_view() { if (is_admin()) { return; } $post_id = get_the_ID(); $post_type = get_post_type(); $event_data = array( 'post_id' => $post_id, 'post_title' => get_the_title(), 'post_type' => $post_type ); maf_track_event('page_view', $event_data); } add_action('wp', 'maf_track_page_view');
-
- 营销漏斗需要明确的阶段定义和转换规则: // 定义漏斗阶段 function maf_get_funnel_stages() { return array( 'awareness' => array( 'name' => '认知阶段', 'description' => '用户首次接触品牌', 'next_stages' => array('interest'), 'triggers' => array('page_view', 'social_click') ), 'interest' => array( 'name' => '兴趣阶段', 'description' => '用户对产品或服务产生兴趣', 'next_stages' => array('consideration'), 'triggers' => array('form_submission', 'content_download') ), 'consideration' => array( 'name' => '考虑阶段', 'description' => '用户评估不同选项', 'next_stages' => array('intent'), 'triggers' => array('product_view', 'pricing_view') ), 'intent' => array( 'name' => '意向阶段', 'description' => '用户表现出购买意愿', 'next_stages' => array('purchase'), 'triggers' => array('cart_add', 'demo_request') ), 'purchase' => array( 'name' => '购买阶段', 'description' => '用户完成交易', 'next_stages' => array('loyalty'), 'triggers' => array('order_complete') ), 'loyalty' => array( 'name' => '忠诚阶段', 'description' => '用户成为忠实客户并推荐他人', 'next_stages' => array(), 'triggers' => array('repeat_purchase', 'referral') ) ); } // 检查并更新客户漏斗阶段 function maf_update_customer_funnel_stage($customer_id, $event_type) { global $wpdb; $funnel_stages = maf_get_funnel_stages(); $table_name = $wpdb->prefix . 'maf_customers'; // 获取客户当前阶段 $current_stage = $wpdb->get_var( $wpdb->prepare( "SELECT funnel_stage FROM $table_name WHERE id = %d", $customer_id ) ); // 检查事件是否触发阶段转换 foreach ($funnel_stages as $stage_id => $stage) { if ($stage_id === $current_stage && in_array($event_type, $stage['triggers'])) { // 移动到下一个阶段 if (!empty($stage['next_stages'])) { $next_stage = $stage['next_stages'][0]; // 简单实现:取第一个下一个阶段 $wpdb->update( $table_name, array('funnel_stage' => $next_stage), array('id' => $customer_id) ); // 记录阶段转换事件 maf_track_event('funnel_stage_change', array( 'from_stage' => $current_stage, 'to_stage' => $next_stage, 'trigger_event' => $event_type )); // 触发阶段转换动作 do_action('maf_funnel_stage_changed', $customer_id, $current_stage, $next_stage); } break; } } }
- 根据客户所在的漏斗阶段,触发相应的营销动作: // 根据漏斗阶段执行自动化动作 function maf_execute_funnel_actions($customer_id, $from_stage, $to_stage) { $actions = array(); switch ($to_stage) { case 'interest': $actions[] = array( 'type' => 'email', 'action' => 'send_welcome_series', 'delay' => 0 ); $actions[] = array( 'type' => 'internal', 'action' => 'tag_customer', 'tag' => 'interested' ); break; case 'consideration': $actions[] = array( 'type' => 'email', 'action' => 'send_case_studies', 'delay' => 1 // 1天后发送 ); $actions[] = array( 'type' => 'internal', 'action' => 'tag_customer', 'tag' => 'considering' ); break; case 'intent': $actions[] = array( 'type' => 'email', 'action' => 'send_demo_offer', 'delay' => 0 ); $actions[] = array( 'type' => 'task', 'action' => 'notify_sales_team', 'delay' => 0 ); break; case 'purchase': $actions[] = array( 'type' => 'email', 'action' => 'send_thank_you', 'delay' => 0 ); $actions[] = array( 'type' => 'email', 'action' => 'send_upsell_offer', 'delay' => 7 // 7天后发送 ); break; } // 执行动作 foreach ($actions as $action) { if ($action['delay'] > 0) { // 计划延迟执行 wp_schedule_single_event( time() + ($action['delay'] * DAY_IN_SECONDS), 'maf_scheduled_action', array($customer_id, $action) ); } else { // 立即执行 maf_execute_single_action($customer_id, $action); } } } add_action('maf_funnel_stage_changed', 'maf_execute_funnel_actions', 10, 3); // 执行单个动作 function maf_execute_single_action($customer_id, $action) { global $wpdb; $table_name = $wpdb->prefix . 'maf_customers'; $customer = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table_name WHERE id = %d", $customer_id ) ); if (!$customer) { return; } switch ($action['type']) { case 'email': maf_send_automated_email($customer, $action['action']); break; case 'internal': if ($action['action'] === 'tag_customer') { // 为客户添加标签 $tags = get_user_meta($customer_id, 'maf_tags', true); if (!is_array($tags)) { $tags = array(); } if (!in_array($action['tag'], $tags)) { $tags[] = $action['tag']; update_user_meta($customer_id, 'maf_tags', $tags); } } break; case 'task': if ($action['action'] === 'notify_sales_team') { // 通知销售团队 $admin_email = get_option('admin_email'); $subject = '新销售线索:客户进入意向阶段'; $message = "客户 {$customer->email} 已进入意向阶段,请及时跟进。"; wp_mail($admin_email, $subject, $message); } break; } // 记录动作执行 maf_track_event('automation_action', array( 'action_type' => $action['type'], 'action_name' => $action['action'], 'customer_id' => $customer_id )); }
-
- 一个直观的管理界面对于营销自动化插件至关重要。我们将创建多级管理菜单,让用户可以轻松访问所有功能: // 添加管理菜单 function maf_add_admin_menu() { // 主菜单 add_menu_page( '营销自动化漏斗', '营销自动化', 'manage_options', 'marketing-automation-funnel', 'maf_dashboard_page', 'dashicons-chart-line', 30 ); // 子菜单项 add_submenu_page( 'marketing-automation-funnel', '仪表板', '仪表板', 'manage_options', 'marketing-automation-funnel', 'maf_dashboard_page' ); add_submenu_page( 'marketing-automation-funnel', '客户旅程', '客户旅程', 'manage_options', 'maf-customer-journeys', 'maf_customer_journeys_page' ); add_submenu_page( 'marketing-automation-funnel', '营销漏斗', '营销漏斗', 'manage_options', 'maf-marketing-funnel', 'maf_marketing_funnel_page' ); add_submenu_page( 'marketing-automation-funnel', '自动化规则', '自动化规则', 'manage_options', 'maf-automation-rules', 'maf_automation_rules_page' ); add_submenu_page( 'marketing-automation-funnel', '设置', '设置', 'manage_options', 'maf-settings', 'maf_settings_page' ); } add_action('admin_menu', 'maf_add_admin_menu'); // 仪表板页面 function maf_dashboard_page() { if (!current_user_can('manage_options')) { wp_die('您没有权限访问此页面'); } ?> <div class="wrap maf-dashboard"> <h1 class="wp-heading-inline">营销自动化仪表板</h1> <div class="maf-stats-container"> <div class="maf-stat-card"> <h3>总客户数</h3> <div class="maf-stat-number"><?php echo maf_get_total_customers(); ?></div> <div class="maf-stat-trend">+12% 较上月</div> </div> <div class="maf-stat-card"> <h3>今日互动</h3> <div class="maf-stat-number"><?php echo maf_get_today_interactions(); ?></div> <div class="maf-stat-trend">+5% 较昨日</div> </div> <div class="maf-stat-card"> <h3>转化率</h3> <div class="maf-stat-number"><?php echo maf_get_conversion_rate(); ?>%</div> <div class="maf-stat-trend">+2.3% 较上周</div> </div> <div class="maf-stat-card"> <h3>平均停留阶段</h3> <div class="maf-stat-number"><?php echo maf_get_avg_stage_duration(); ?>天</div> <div class="maf-stat-trend">-1.2天 较上月</div> </div> </div> <div class="maf-charts-container"> <div class="maf-chart-card"> <h3>漏斗阶段分布</h3> <div class="maf-chart" id="funnel-stage-chart"></div> </div> <div class="maf-chart-card"> <h3>客户增长趋势</h3> <div class="maf-chart" id="customer-growth-chart"></div> </div> </div> <div class="maf-recent-activity"> <h3>最近活动</h3> <table class="wp-list-table widefat fixed striped"> <thead> <tr> <th>时间</th> <th>客户</th> <th>事件</th> <th>页面</th> <th>阶段变化</th> </tr> </thead> <tbody> <?php echo maf_get_recent_activities_html(); ?> </tbody> </table> </div> </div> <script> // 使用Chart.js渲染图表 jQuery(document).ready(function($) { // 漏斗阶段分布图 var funnelCtx = document.getElementById('funnel-stage-chart').getContext('2d'); var funnelChart = new Chart(funnelCtx, { type: 'doughnut', data: { labels: <?php echo json_encode(maf_get_funnel_stage_labels()); ?>, datasets: [{ data: <?php echo json_encode(maf_get_funnel_stage_counts()); ?>, backgroundColor: [ '#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF', '#FF9F40' ] }] }, options: { responsive: true, maintainAspectRatio: false } }); // 客户增长趋势图 var growthCtx = document.getElementById('customer-growth-chart').getContext('2d'); var growthChart = new Chart(growthCtx, { type: 'line', data: { labels: <?php echo json_encode(maf_get_last_30_days_labels()); ?>, datasets: [{ label: '新客户', data: <?php echo json_encode(maf_get_new_customers_last_30_days()); ?>, borderColor: '#36A2EB', fill: false }, { label: '总客户', data: <?php echo json_encode(maf_get_total_customers_last_30_days()); ?>, borderColor: '#FF6384', fill: false }] }, options: { responsive: true, maintainAspectRatio: false, scales: { y: { beginAtZero: true } } } }); }); </script> <?php }
- 创建详细的客户旅程查看界面,让营销人员能够深入了解每个客户的行为路径: // 客户旅程页面 function maf_customer_journeys_page() { if (!current_user_can('manage_options')) { wp_die('您没有权限访问此页面'); } global $wpdb; // 分页参数 $per_page = 20; $current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1; $offset = ($current_page - 1) * $per_page; // 搜索功能 $search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : ''; $where_clause = ''; if (!empty($search)) { $where_clause = $wpdb->prepare( " WHERE email LIKE %s OR first_name LIKE %s OR last_name LIKE %s", '%' . $wpdb->esc_like($search) . '%', '%' . $wpdb->esc_like($search) . '%', '%' . $wpdb->esc_like($search) . '%' ); } // 获取客户总数 $table_name = $wpdb->prefix . 'maf_customers'; $total_customers = $wpdb->get_var("SELECT COUNT(*) FROM $table_name $where_clause"); $total_pages = ceil($total_customers / $per_page); // 获取客户列表 $customers = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $table_name $where_clause ORDER BY last_seen DESC LIMIT %d OFFSET %d", $per_page, $offset ) ); ?> <div class="wrap"> <h1 class="wp-heading-inline">客户旅程</h1> <form method="get" class="search-form"> <input type="hidden" name="page" value="maf-customer-journeys"> <p class="search-box"> <label class="screen-reader-text" for="customer-search">搜索客户:</label> <input type="search" id="customer-search" name="s" value="<?php echo esc_attr($search); ?>"> <input type="submit" id="search-submit" class="button" value="搜索客户"> </p> </form> <div class="tablenav top"> <div class="tablenav-pages"> <span class="displaying-num"><?php echo $total_customers; ?> 个客户</span> <?php if ($total_pages > 1): ?> <span class="pagination-links"> <?php echo paginate_links(array( 'base' => add_query_arg('paged', '%#%'), 'format' => '', 'prev_text' => '«', 'next_text' => '»', 'total' => $total_pages, 'current' => $current_page )); ?> </span> <?php endif; ?> </div> </div> <table class="wp-list-table widefat fixed striped"> <thead> <tr> <th>ID</th> <th>邮箱</th> <th>姓名</th> <th>当前阶段</th> <th>首次访问</th> <th>最后活动</th> <th>总互动数</th> <th>操作</th> </tr> </thead> <tbody> <?php foreach ($customers as $customer): ?> <tr> <td><?php echo $customer->id; ?></td> <td> <strong><?php echo esc_html($customer->email); ?></strong> <?php if (strpos($customer->email, '@anonymous.com') !== false): ?> <span class="dashicons dashicons-admin-users" title="匿名用户"></span> <?php endif; ?> </td> <td><?php echo esc_html($customer->first_name . ' ' . $customer->last_name); ?></td> <td> <?php $stage_info = maf_get_funnel_stage_info($customer->funnel_stage); echo '<span class="maf-stage-badge maf-stage-' . $customer->funnel_stage . '">' . esc_html($stage_info['name']) . '</span>'; ?> </td> <td><?php echo date('Y-m-d H:i', strtotime($customer->created_at)); ?></td> <td><?php echo $customer->last_seen ? date('Y-m-d H:i', strtotime($customer->last_seen)) : '从未'; ?></td> <td><?php echo maf_get_customer_interaction_count($customer->id); ?></td> <td> <a href="<?php echo admin_url('admin.php?page=maf-customer-journeys&view=customer&id=' . $customer->id); ?>" class="button button-small">查看旅程</a> <button class="button button-small maf-send-message" data-customer-id="<?php echo $customer->id; ?>" data-customer-email="<?php echo esc_attr($customer->email); ?>"> 发送消息 </button> </td> </tr> <?php endforeach; ?> </tbody> </table> </div> <!-- 发送消息模态框 --> <div id="maf-message-modal" class="maf-modal" style="display:none;"> <div class="maf-modal-content"> <div class="maf-modal-header"> <h3>发送消息给客户</h3> <span class="maf-modal-close">×</span> </div> <div class="maf-modal-body"> <form id="maf-message-form"> <input type="hidden" id="maf-message-customer-id" name="customer_id"> <div class="maf-form-group"> <label for="maf-message-subject">主题:</label> <input type="text" id="maf-message-subject" name="subject" class="regular-text" required> </div> <div class="maf-form-group"> <label for="maf-message-content">内容:</label> <textarea id="maf-message-content" name="content" rows="6" class="large-text" required></textarea> </div> <div class="maf-form-group"> <label for="maf-message-type">消息类型:</label> <select id="maf-message-type" name="message_type"> <option value="email">电子邮件</option> <option value="notification">站内通知</option> <option value="sms">短信</option> </select> </div> <div class="maf-form-actions"> <button type="submit" class="button button-primary">发送</button> <button type="button" class="button maf-modal-cancel">取消</button> </div> </form> </div> </div> </div> <script> jQuery(document).ready(function($) { // 发送消息模态框 $('.maf-send-message').click(function() { var customerId = $(this).data('customer-id'); var customerEmail = $(this).data('customer-email'); $('#maf-message-customer-id').val(customerId); $('#maf-message-subject').val('来自 ' + '<?php echo get_bloginfo("name"); ?> 的消息'); $('#maf-message-modal').show(); }); $('.maf-modal-close, .maf-modal-cancel').click(function() { $('#maf-message-modal').hide(); }); // 发送消息表单提交 $('#maf-message-form').submit(function(e) { e.preventDefault(); var formData = $(this).serialize(); $.ajax({ url: ajaxurl, type: 'POST', data: { action: 'maf_send_customer_message', nonce: '<?php echo wp_create_nonce("maf_send_message"); ?>', form_data: formData }, beforeSend: function() { $('.maf-form-actions button').prop('disabled', true).text('发送中...'); }, success: function(response) { if (response.success) { alert('消息发送成功!'); $('#maf-message-modal').hide(); $('#maf-message-form')[0].reset(); } else { alert('发送失败: ' + response.data); } }, complete: function() { $('.maf-form-actions button').prop('disabled', false).text('发送'); } }); }); }); </script> <?php }
- 创建可视化漏斗分析页面,帮助用户理解转化路径和瓶颈: // 营销漏斗分析页面 function maf_marketing_funnel_page() { if (!current_user_can('manage_options')) { wp_die('您没有权限访问此页面'); } global $wpdb; // 获取时间段参数 $time_range = isset($_GET['time_range']) ? sanitize_text_field($_GET['time_range']) : '30days'; $start_date = maf_calculate_start_date($time_range); // 获取漏斗数据 $funnel_data = maf_get_funnel_analysis_data($start_date); ?> <div class="wrap"> <h1 class="wp-heading-inline">营销漏斗分析</h1> <div class="maf-funnel-controls"> <form method="get" class="maf-time-range-form"> <input type="hidden" name="page" value="maf-marketing-funnel"> <label for="time-range">时间范围:</label> <select name="time_range" id="time-range" onchange="this.form.submit()"> <option value="7days" <?php selected($time_range, '7days'); ?>>最近7天</option> <option value="30days" <?php selected($time_range, '30days'); ?>>最近30天</option> <option value="90days" <?php selected($time_range, '90days'); ?>>最近90天</option> <option value="custom" <?php selected($time_range, 'custom'); ?>>自定义</option> </select> <?php if ($time_range === 'custom'): ?> <label for="start-date">开始日期:</label> <input type="date" name="start_date" id="start-date" value="<?php echo isset($_GET['start_date']) ? esc_attr($_GET['start_date']) : ''; ?>"> <label for="end-date">结束日期:</label> <input type="date" name="end_date" id="end-date" value="<?php echo isset($_GET['end_date']) ? esc_attr($_GET['end_date']) : ''; ?>"> <input type="submit" class="button" value="应用"> <?php endif; ?> </form> </div> <div class="maf-funnel-visualization"> <h3>漏斗可视化</h3> <div class="maf-funnel-chart-container"> <div class="maf-funnel-chart"> <?php foreach ($funnel_data['stages'] as $stage): ?>
在当今数字化营销时代,网站不仅仅是企业的线上名片,更是潜在客户转化的重要渠道。WordPress作为全球最受欢迎的内容管理系统,其强大的可扩展性为开发者提供了无限可能。通过自定义插件开发,我们可以将WordPress从一个简单的博客平台转变为功能强大的营销自动化工具。
本教程将深入探讨如何通过WordPress插件开发,实现网站自动化营销漏斗与客户旅程追踪功能。我们将从基础概念讲起,逐步构建一个完整的营销自动化插件,帮助您更好地理解客户行为,优化转化路径,并最终提升业务成果。
营销漏斗是描述潜在客户从认知到购买决策过程的模型。传统营销漏斗通常包括以下阶段:
- 认知阶段:用户首次接触品牌
- 兴趣阶段:用户对产品或服务产生兴趣
- 考虑阶段:用户评估不同选项
- 意向阶段:用户表现出购买意愿
- 购买阶段:用户完成交易
- 忠诚阶段:用户成为忠实客户并推荐他人
客户旅程追踪是指记录和分析用户与品牌互动的全过程。通过追踪客户旅程,企业可以:
- 识别转化路径中的瓶颈
- 个性化用户体验
- 优化营销策略
- 提高客户留存率
- 增加客户生命周期价值
WordPress作为开发平台具有以下优势:
- 庞大的用户基础和成熟的生态系统
- 灵活的插件架构和丰富的API
- 开源特性,允许深度定制
- 与各种第三方服务的良好兼容性
- 强大的社区支持和丰富的学习资源
在开始开发之前,我们需要准备以下环境:
- 本地开发环境:推荐使用XAMPP、MAMP或Local by Flywheel
- 代码编辑器:VS Code、PHPStorm或Sublime Text
- WordPress安装:最新版本的WordPress
- 调试工具:安装Query Monitor、Debug Bar等调试插件
每个WordPress插件至少需要一个主文件,通常以插件名称命名。以下是创建基础插件结构的步骤:
- 在
wp-content/plugins/目录下创建新文件夹,命名为marketing-automation-funnel - 在该文件夹中创建主文件
marketing-automation-funnel.php - 添加插件头部信息:
<?php
/**
* Plugin Name: 营销自动化漏斗与客户旅程追踪
* Plugin URI: https://yourwebsite.com/marketing-automation-funnel
* Description: 实现网站自动化营销漏斗与客户旅程追踪功能
* Version: 1.0.0
* Author: 您的姓名
* Author URI: https://yourwebsite.com
* License: GPL v2 or later
* Text Domain: marketing-automation-funnel
*/
了解WordPress插件的基本架构是开发的关键:
- 钩子系统:动作钩子(Actions)和过滤器钩子(Filters)
- 短代码系统:允许在文章和页面中嵌入动态内容
- 自定义数据库表:存储插件特定数据
- 管理界面:为插件创建设置页面
- AJAX处理:实现前端与后端的异步通信
客户旅程数据需要专门的数据库表来存储。我们将创建两个主要表:
- 客户表:存储客户基本信息
- 互动事件表:记录客户的每一次互动
以下是创建数据库表的代码示例:
// 在插件激活时创建数据库表
register_activation_hook(__FILE__, 'maf_create_tables');
function maf_create_tables() {
global $wpdb;
$charset_collate = $wpdb->get_charset_collate();
// 客户表
$table_name_customers = $wpdb->prefix . 'maf_customers';
$sql_customers = "CREATE TABLE IF NOT EXISTS $table_name_customers (
id INT(11) NOT NULL AUTO_INCREMENT,
email VARCHAR(100) NOT NULL,
first_name VARCHAR(50),
last_name VARCHAR(50),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_seen DATETIME,
funnel_stage VARCHAR(50) DEFAULT 'awareness',
PRIMARY KEY (id),
UNIQUE KEY email (email)
) $charset_collate;";
// 互动事件表
$table_name_events = $wpdb->prefix . 'maf_events';
$sql_events = "CREATE TABLE IF NOT EXISTS $table_name_events (
id INT(11) NOT NULL AUTO_INCREMENT,
customer_id INT(11) NOT NULL,
event_type VARCHAR(50) NOT NULL,
event_data TEXT,
page_url VARCHAR(500),
referrer VARCHAR(500),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
KEY customer_id (customer_id),
KEY event_type (event_type)
) $charset_collate;";
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql_customers);
dbDelta($sql_events);
}
客户识别是旅程追踪的基础。我们将通过以下方式识别客户:
- Cookie追踪:为匿名用户设置唯一标识符
- 用户登录识别:对已登录用户直接识别
- 电子邮件识别:通过表单提交获取电子邮件
// 设置客户Cookie
function maf_set_customer_cookie() {
if (!isset($_COOKIE['maf_customer_id'])) {
$customer_id = uniqid('maf_', true);
setcookie('maf_customer_id', $customer_id, time() + (365 * 24 * 60 * 60), '/');
}
}
add_action('init', 'maf_set_customer_cookie');
// 识别当前客户
function maf_identify_customer() {
global $wpdb;
$customer_id = null;
// 检查是否已登录
if (is_user_logged_in()) {
$user_id = get_current_user_id();
$user_info = get_userdata($user_id);
// 检查用户是否已存在于客户表中
$table_name = $wpdb->prefix . 'maf_customers';
$existing_customer = $wpdb->get_row(
$wpdb->prepare(
"SELECT id FROM $table_name WHERE email = %s",
$user_info->user_email
)
);
if ($existing_customer) {
$customer_id = $existing_customer->id;
} else {
// 创建新客户记录
$wpdb->insert(
$table_name,
array(
'email' => $user_info->user_email,
'first_name' => $user_info->first_name,
'last_name' => $user_info->last_name,
'created_at' => current_time('mysql')
)
);
$customer_id = $wpdb->insert_id;
}
}
// 检查Cookie
elseif (isset($_COOKIE['maf_customer_id'])) {
$cookie_id = sanitize_text_field($_COOKIE['maf_customer_id']);
// 查找基于Cookie的客户记录
$table_name = $wpdb->prefix . 'maf_customers';
$existing_customer = $wpdb->get_row(
$wpdb->prepare(
"SELECT id FROM $table_name WHERE email = %s",
$cookie_id . '@anonymous.com'
)
);
if ($existing_customer) {
$customer_id = $existing_customer->id;
}
}
return $customer_id;
}
客户与网站的每一次互动都应该被记录:
// 记录客户事件
function maf_track_event($event_type, $event_data = array()) {
global $wpdb;
$customer_id = maf_identify_customer();
if (!$customer_id) {
// 为匿名用户创建临时记录
$table_name = $wpdb->prefix . 'maf_customers';
$cookie_id = isset($_COOKIE['maf_customer_id']) ? sanitize_text_field($_COOKIE['maf_customer_id']) : uniqid('maf_', true);
$wpdb->insert(
$table_name,
array(
'email' => $cookie_id . '@anonymous.com',
'created_at' => current_time('mysql'),
'last_seen' => current_time('mysql')
)
);
$customer_id = $wpdb->insert_id;
}
// 更新客户最后访问时间
$wpdb->update(
$wpdb->prefix . 'maf_customers',
array('last_seen' => current_time('mysql')),
array('id' => $customer_id)
);
// 记录事件
$table_name = $wpdb->prefix . 'maf_events';
$wpdb->insert(
$table_name,
array(
'customer_id' => $customer_id,
'event_type' => sanitize_text_field($event_type),
'event_data' => maybe_serialize($event_data),
'page_url' => isset($_SERVER['REQUEST_URI']) ? esc_url_raw($_SERVER['REQUEST_URI']) : '',
'referrer' => isset($_SERVER['HTTP_REFERER']) ? esc_url_raw($_SERVER['HTTP_REFERER']) : '',
'created_at' => current_time('mysql')
)
);
return $wpdb->insert_id;
}
// 自动追踪页面访问
function maf_track_page_view() {
if (is_admin()) {
return;
}
$post_id = get_the_ID();
$post_type = get_post_type();
$event_data = array(
'post_id' => $post_id,
'post_title' => get_the_title(),
'post_type' => $post_type
);
maf_track_event('page_view', $event_data);
}
add_action('wp', 'maf_track_page_view');
营销漏斗需要明确的阶段定义和转换规则:
// 定义漏斗阶段
function maf_get_funnel_stages() {
return array(
'awareness' => array(
'name' => '认知阶段',
'description' => '用户首次接触品牌',
'next_stages' => array('interest'),
'triggers' => array('page_view', 'social_click')
),
'interest' => array(
'name' => '兴趣阶段',
'description' => '用户对产品或服务产生兴趣',
'next_stages' => array('consideration'),
'triggers' => array('form_submission', 'content_download')
),
'consideration' => array(
'name' => '考虑阶段',
'description' => '用户评估不同选项',
'next_stages' => array('intent'),
'triggers' => array('product_view', 'pricing_view')
),
'intent' => array(
'name' => '意向阶段',
'description' => '用户表现出购买意愿',
'next_stages' => array('purchase'),
'triggers' => array('cart_add', 'demo_request')
),
'purchase' => array(
'name' => '购买阶段',
'description' => '用户完成交易',
'next_stages' => array('loyalty'),
'triggers' => array('order_complete')
),
'loyalty' => array(
'name' => '忠诚阶段',
'description' => '用户成为忠实客户并推荐他人',
'next_stages' => array(),
'triggers' => array('repeat_purchase', 'referral')
)
);
}
// 检查并更新客户漏斗阶段
function maf_update_customer_funnel_stage($customer_id, $event_type) {
global $wpdb;
$funnel_stages = maf_get_funnel_stages();
$table_name = $wpdb->prefix . 'maf_customers';
// 获取客户当前阶段
$current_stage = $wpdb->get_var(
$wpdb->prepare(
"SELECT funnel_stage FROM $table_name WHERE id = %d",
$customer_id
)
);
// 检查事件是否触发阶段转换
foreach ($funnel_stages as $stage_id => $stage) {
if ($stage_id === $current_stage && in_array($event_type, $stage['triggers'])) {
// 移动到下一个阶段
if (!empty($stage['next_stages'])) {
$next_stage = $stage['next_stages'][0]; // 简单实现:取第一个下一个阶段
$wpdb->update(
$table_name,
array('funnel_stage' => $next_stage),
array('id' => $customer_id)
);
// 记录阶段转换事件
maf_track_event('funnel_stage_change', array(
'from_stage' => $current_stage,
'to_stage' => $next_stage,
'trigger_event' => $event_type
));
// 触发阶段转换动作
do_action('maf_funnel_stage_changed', $customer_id, $current_stage, $next_stage);
}
break;
}
}
}
根据客户所在的漏斗阶段,触发相应的营销动作:
// 根据漏斗阶段执行自动化动作
function maf_execute_funnel_actions($customer_id, $from_stage, $to_stage) {
$actions = array();
switch ($to_stage) {
case 'interest':
$actions[] = array(
'type' => 'email',
'action' => 'send_welcome_series',
'delay' => 0
);
$actions[] = array(
'type' => 'internal',
'action' => 'tag_customer',
'tag' => 'interested'
);
break;
case 'consideration':
$actions[] = array(
'type' => 'email',
'action' => 'send_case_studies',
'delay' => 1 // 1天后发送
);
$actions[] = array(
'type' => 'internal',
'action' => 'tag_customer',
'tag' => 'considering'
);
break;
case 'intent':
$actions[] = array(
'type' => 'email',
'action' => 'send_demo_offer',
'delay' => 0
);
$actions[] = array(
'type' => 'task',
'action' => 'notify_sales_team',
'delay' => 0
);
break;
case 'purchase':
$actions[] = array(
'type' => 'email',
'action' => 'send_thank_you',
'delay' => 0
);
$actions[] = array(
'type' => 'email',
'action' => 'send_upsell_offer',
'delay' => 7 // 7天后发送
);
break;
}
// 执行动作
foreach ($actions as $action) {
if ($action['delay'] > 0) {
// 计划延迟执行
wp_schedule_single_event(
time() + ($action['delay'] * DAY_IN_SECONDS),
'maf_scheduled_action',
array($customer_id, $action)
);
} else {
// 立即执行
maf_execute_single_action($customer_id, $action);
}
}
}
add_action('maf_funnel_stage_changed', 'maf_execute_funnel_actions', 10, 3);
// 执行单个动作
function maf_execute_single_action($customer_id, $action) {
global $wpdb;
$table_name = $wpdb->prefix . 'maf_customers';
$customer = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM $table_name WHERE id = %d",
$customer_id
)
);
if (!$customer) {
return;
}
switch ($action['type']) {
case 'email':
maf_send_automated_email($customer, $action['action']);
break;
case 'internal':
if ($action['action'] === 'tag_customer') {
// 为客户添加标签
$tags = get_user_meta($customer_id, 'maf_tags', true);
if (!is_array($tags)) {
$tags = array();
}
if (!in_array($action['tag'], $tags)) {
$tags[] = $action['tag'];
update_user_meta($customer_id, 'maf_tags', $tags);
}
}
break;
case 'task':
if ($action['action'] === 'notify_sales_team') {
// 通知销售团队
$admin_email = get_option('admin_email');
$subject = '新销售线索:客户进入意向阶段';
$message = "客户 {$customer->email} 已进入意向阶段,请及时跟进。";
wp_mail($admin_email, $subject, $message);
}
break;
}
// 记录动作执行
maf_track_event('automation_action', array(
'action_type' => $action['type'],
'action_name' => $action['action'],
'customer_id' => $customer_id
));
}
一个直观的管理界面对于营销自动化插件至关重要。我们将创建多级管理菜单,让用户可以轻松访问所有功能:
// 添加管理菜单
function maf_add_admin_menu() {
// 主菜单
add_menu_page(
'营销自动化漏斗',
'营销自动化',
'manage_options',
'marketing-automation-funnel',
'maf_dashboard_page',
'dashicons-chart-line',
30
);
// 子菜单项
add_submenu_page(
'marketing-automation-funnel',
'仪表板',
'仪表板',
'manage_options',
'marketing-automation-funnel',
'maf_dashboard_page'
);
add_submenu_page(
'marketing-automation-funnel',
'客户旅程',
'客户旅程',
'manage_options',
'maf-customer-journeys',
'maf_customer_journeys_page'
);
add_submenu_page(
'marketing-automation-funnel',
'营销漏斗',
'营销漏斗',
'manage_options',
'maf-marketing-funnel',
'maf_marketing_funnel_page'
);
add_submenu_page(
'marketing-automation-funnel',
'自动化规则',
'自动化规则',
'manage_options',
'maf-automation-rules',
'maf_automation_rules_page'
);
add_submenu_page(
'marketing-automation-funnel',
'设置',
'设置',
'manage_options',
'maf-settings',
'maf_settings_page'
);
}
add_action('admin_menu', 'maf_add_admin_menu');
// 仪表板页面
function maf_dashboard_page() {
if (!current_user_can('manage_options')) {
wp_die('您没有权限访问此页面');
}
?>
<div class="wrap maf-dashboard">
<h1 class="wp-heading-inline">营销自动化仪表板</h1>
<div class="maf-stats-container">
<div class="maf-stat-card">
<h3>总客户数</h3>
<div class="maf-stat-number"><?php echo maf_get_total_customers(); ?></div>
<div class="maf-stat-trend">+12% 较上月</div>
</div>
<div class="maf-stat-card">
<h3>今日互动</h3>
<div class="maf-stat-number"><?php echo maf_get_today_interactions(); ?></div>
<div class="maf-stat-trend">+5% 较昨日</div>
</div>
<div class="maf-stat-card">
<h3>转化率</h3>
<div class="maf-stat-number"><?php echo maf_get_conversion_rate(); ?>%</div>
<div class="maf-stat-trend">+2.3% 较上周</div>
</div>
<div class="maf-stat-card">
<h3>平均停留阶段</h3>
<div class="maf-stat-number"><?php echo maf_get_avg_stage_duration(); ?>天</div>
<div class="maf-stat-trend">-1.2天 较上月</div>
</div>
</div>
<div class="maf-charts-container">
<div class="maf-chart-card">
<h3>漏斗阶段分布</h3>
<div class="maf-chart" id="funnel-stage-chart"></div>
</div>
<div class="maf-chart-card">
<h3>客户增长趋势</h3>
<div class="maf-chart" id="customer-growth-chart"></div>
</div>
</div>
<div class="maf-recent-activity">
<h3>最近活动</h3>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>时间</th>
<th>客户</th>
<th>事件</th>
<th>页面</th>
<th>阶段变化</th>
</tr>
</thead>
<tbody>
<?php echo maf_get_recent_activities_html(); ?>
</tbody>
</table>
</div>
</div>
<script>
// 使用Chart.js渲染图表
jQuery(document).ready(function($) {
// 漏斗阶段分布图
var funnelCtx = document.getElementById('funnel-stage-chart').getContext('2d');
var funnelChart = new Chart(funnelCtx, {
type: 'doughnut',
data: {
labels: <?php echo json_encode(maf_get_funnel_stage_labels()); ?>,
datasets: [{
data: <?php echo json_encode(maf_get_funnel_stage_counts()); ?>,
backgroundColor: [
'#FF6384', '#36A2EB', '#FFCE56', '#4BC0C0', '#9966FF', '#FF9F40'
]
}]
},
options: {
responsive: true,
maintainAspectRatio: false
}
});
// 客户增长趋势图
var growthCtx = document.getElementById('customer-growth-chart').getContext('2d');
var growthChart = new Chart(growthCtx, {
type: 'line',
data: {
labels: <?php echo json_encode(maf_get_last_30_days_labels()); ?>,
datasets: [{
label: '新客户',
data: <?php echo json_encode(maf_get_new_customers_last_30_days()); ?>,
borderColor: '#36A2EB',
fill: false
}, {
label: '总客户',
data: <?php echo json_encode(maf_get_total_customers_last_30_days()); ?>,
borderColor: '#FF6384',
fill: false
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
}
});
});
</script>
<?php
}
创建详细的客户旅程查看界面,让营销人员能够深入了解每个客户的行为路径:
// 客户旅程页面
function maf_customer_journeys_page() {
if (!current_user_can('manage_options')) {
wp_die('您没有权限访问此页面');
}
global $wpdb;
// 分页参数
$per_page = 20;
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1;
$offset = ($current_page - 1) * $per_page;
// 搜索功能
$search = isset($_GET['s']) ? sanitize_text_field($_GET['s']) : '';
$where_clause = '';
if (!empty($search)) {
$where_clause = $wpdb->prepare(
" WHERE email LIKE %s OR first_name LIKE %s OR last_name LIKE %s",
'%' . $wpdb->esc_like($search) . '%',
'%' . $wpdb->esc_like($search) . '%',
'%' . $wpdb->esc_like($search) . '%'
);
}
// 获取客户总数
$table_name = $wpdb->prefix . 'maf_customers';
$total_customers = $wpdb->get_var("SELECT COUNT(*) FROM $table_name $where_clause");
$total_pages = ceil($total_customers / $per_page);
// 获取客户列表
$customers = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM $table_name $where_clause ORDER BY last_seen DESC LIMIT %d OFFSET %d",
$per_page,
$offset
)
);
?>
<div class="wrap">
<h1 class="wp-heading-inline">客户旅程</h1>
<form method="get" class="search-form">
<input type="hidden" name="page" value="maf-customer-journeys">
<p class="search-box">
<label class="screen-reader-text" for="customer-search">搜索客户:</label>
<input type="search" id="customer-search" name="s" value="<?php echo esc_attr($search); ?>">
<input type="submit" id="search-submit" class="button" value="搜索客户">
</p>
</form>
<div class="tablenav top">
<div class="tablenav-pages">
<span class="displaying-num"><?php echo $total_customers; ?> 个客户</span>
<?php if ($total_pages > 1): ?>
<span class="pagination-links">
<?php
echo paginate_links(array(
'base' => add_query_arg('paged', '%#%'),
'format' => '',
'prev_text' => '«',
'next_text' => '»',
'total' => $total_pages,
'current' => $current_page
));
?>
</span>
<?php endif; ?>
</div>
</div>
<table class="wp-list-table widefat fixed striped">
<thead>
<tr>
<th>ID</th>
<th>邮箱</th>
<th>姓名</th>
<th>当前阶段</th>
<th>首次访问</th>
<th>最后活动</th>
<th>总互动数</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($customers as $customer): ?>
<tr>
<td><?php echo $customer->id; ?></td>
<td>
<strong><?php echo esc_html($customer->email); ?></strong>
<?php if (strpos($customer->email, '@anonymous.com') !== false): ?>
<span class="dashicons dashicons-admin-users" title="匿名用户"></span>
<?php endif; ?>
</td>
<td><?php echo esc_html($customer->first_name . ' ' . $customer->last_name); ?></td>
<td>
<?php
$stage_info = maf_get_funnel_stage_info($customer->funnel_stage);
echo '<span class="maf-stage-badge maf-stage-' . $customer->funnel_stage . '">' .
esc_html($stage_info['name']) . '</span>';
?>
</td>
<td><?php echo date('Y-m-d H:i', strtotime($customer->created_at)); ?></td>
<td><?php echo $customer->last_seen ? date('Y-m-d H:i', strtotime($customer->last_seen)) : '从未'; ?></td>
<td><?php echo maf_get_customer_interaction_count($customer->id); ?></td>
<td>
<a href="<?php echo admin_url('admin.php?page=maf-customer-journeys&view=customer&id=' . $customer->id); ?>"
class="button button-small">查看旅程</a>
<button class="button button-small maf-send-message"
data-customer-id="<?php echo $customer->id; ?>"
data-customer-email="<?php echo esc_attr($customer->email); ?>">
发送消息
</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
<!-- 发送消息模态框 -->
<div id="maf-message-modal" class="maf-modal" style="display:none;">
<div class="maf-modal-content">
<div class="maf-modal-header">
<h3>发送消息给客户</h3>
<span class="maf-modal-close">×</span>
</div>
<div class="maf-modal-body">
<form id="maf-message-form">
<input type="hidden" id="maf-message-customer-id" name="customer_id">
<div class="maf-form-group">
<label for="maf-message-subject">主题:</label>
<input type="text" id="maf-message-subject" name="subject" class="regular-text" required>
</div>
<div class="maf-form-group">
<label for="maf-message-content">内容:</label>
<textarea id="maf-message-content" name="content" rows="6" class="large-text" required></textarea>
</div>
<div class="maf-form-group">
<label for="maf-message-type">消息类型:</label>
<select id="maf-message-type" name="message_type">
<option value="email">电子邮件</option>
<option value="notification">站内通知</option>
<option value="sms">短信</option>
</select>
</div>
<div class="maf-form-actions">
<button type="submit" class="button button-primary">发送</button>
<button type="button" class="button maf-modal-cancel">取消</button>
</div>
</form>
</div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
// 发送消息模态框
$('.maf-send-message').click(function() {
var customerId = $(this).data('customer-id');
var customerEmail = $(this).data('customer-email');
$('#maf-message-customer-id').val(customerId);
$('#maf-message-subject').val('来自 ' + '<?php echo get_bloginfo("name"); ?> 的消息');
$('#maf-message-modal').show();
});
$('.maf-modal-close, .maf-modal-cancel').click(function() {
$('#maf-message-modal').hide();
});
// 发送消息表单提交
$('#maf-message-form').submit(function(e) {
e.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'maf_send_customer_message',
nonce: '<?php echo wp_create_nonce("maf_send_message"); ?>',
form_data: formData
},
beforeSend: function() {
$('.maf-form-actions button').prop('disabled', true).text('发送中...');
},
success: function(response) {
if (response.success) {
alert('消息发送成功!');
$('#maf-message-modal').hide();
$('#maf-message-form')[0].reset();
} else {
alert('发送失败: ' + response.data);
}
},
complete: function() {
$('.maf-form-actions button').prop('disabled', false).text('发送');
}
});
});
});
</script>
<?php
}
创建可视化漏斗分析页面,帮助用户理解转化路径和瓶颈:
// 营销漏斗分析页面
function maf_marketing_funnel_page() {
if (!current_user_can('manage_options')) {
wp_die('您没有权限访问此页面');
}
global $wpdb;
// 获取时间段参数
$time_range = isset($_GET['time_range']) ? sanitize_text_field($_GET['time_range']) : '30days';
$start_date = maf_calculate_start_date($time_range);
// 获取漏斗数据
$funnel_data = maf_get_funnel_analysis_data($start_date);
?>
<div class="wrap">
<h1 class="wp-heading-inline">营销漏斗分析</h1>
<div class="maf-funnel-controls">
<form method="get" class="maf-time-range-form">
<input type="hidden" name="page" value="maf-marketing-funnel">
<label for="time-range">时间范围:</label>
<select name="time_range" id="time-range" onchange="this.form.submit()">
<option value="7days" <?php selected($time_range, '7days'); ?>>最近7天</option>
<option value="30days" <?php selected($time_range, '30days'); ?>>最近30天</option>
<option value="90days" <?php selected($time_range, '90days'); ?>>最近90天</option>
<option value="custom" <?php selected($time_range, 'custom'); ?>>自定义</option>
</select>
<?php if ($time_range === 'custom'): ?>
<label for="start-date">开始日期:</label>
<input type="date" name="start_date" id="start-date"
value="<?php echo isset($_GET['start_date']) ? esc_attr($_GET['start_date']) : ''; ?>">
<label for="end-date">结束日期:</label>
<input type="date" name="end_date" id="end-date"
value="<?php echo isset($_GET['end_date']) ? esc_attr($_GET['end_date']) : ''; ?>">
<input type="submit" class="button" value="应用">
<?php endif; ?>
</form>
</div>
<div class="maf-funnel-visualization">
<h3>漏斗可视化</h3>
<div class="maf-funnel-chart-container">
<div class="maf-funnel-chart">
<?php foreach ($funnel_data['stages'] as $stage): ?>


