php - 在 PHP 中添加 Google Analytics 事件以提交表单

标签 php forms google-analytics

我希望在 PHP(购买的脚本)中成功提交表单时添加一个事件。我在 PHP 中找到了成功触发的函数,我标记了我认为分析的位置......

function emailSuccessfullySent() {
global $translations, $lang, $options, $fdata, $is_ajax;

// redirect to custom "success" page if it's been set
if ( !empty($options['redirect_url'])) {
    if (!$is_ajax) {
        header('Location: '.$options['redirect_url']);
    } else {
        echo json_encode(array('redirect' => array($options['redirect_url'])));
    }
    exit;
}

// if no redirect has been set, echo out the success message
if ($is_ajax) {
    echo json_encode(array('success' => array($translations->form->success->title->$lang)));
    // analytics code here
} else {
    echo '<h2>'.$translations->form->success->title->$lang.'</h2>';
    // analytics code here
}

removeUploadsFromServer();
}

...但是我不确定如何触发事件 JS:

ga('send', 'event', 'Form Button', 'submit', 'Feedback');

最佳答案

您需要查看 Google Analytics server-side measurement protocol .

这是我成功使用的 PHP 实现,但对于您的用例来说可能有些过分(但至少它是一个引用)。 Here is the full code ,但我在这篇文章中对其进行了简化。

//Handle the parsing of the _ga cookie or setting it to a unique identifier
function ga_parse_cookie(){
    if ( isset($_COOKIE['_ga']) ){
        list($version, $domainDepth, $cid1, $cid2) = explode('.', $_COOKIE["_ga"], 4);
        $contents = array('version' => $version, 'domainDepth' => $domainDepth, 'cid' => $cid1 . '.' . $cid2);
        $cid = $contents['cid'];
    } else {
        $cid = ga_generate_UUID();
    }
    return $cid;
}

//Generate UUID v4 function (needed to generate a CID when one isn't available)
function ga_generate_UUID(){
    return sprintf(
        '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), //32 bits for "time_low"
        mt_rand(0, 0xffff), //16 bits for "time_mid"
        mt_rand(0, 0x0fff) | 0x4000, //16 bits for "time_hi_and_version", Four most significant bits holds version number 4
        mt_rand(0, 0x3fff) | 0x8000, //16 bits, 8 bits for "clk_seq_hi_res", 8 bits for "clk_seq_low", Two most significant bits holds zero and one for variant DCE1.1
        mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) //48 bits for "node"
    );
}

//Send Data to Google Analytics
//https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#event
function ga_send_data($data){
    $getString = 'https://ssl.google-analytics.com/collect';
    $getString .= '?payload_data&';
    $getString .= http_build_query($data);
    $result = wp_remote_get($getString);
    return $result;
}

//Send Event Function for Server-Side Google Analytics
function ga_send_event($category=null, $action=null, $label=null, $value=null, $ni=1){
    //GA Parameter Guide: https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters?hl=en
    //GA Hit Builder: https://ga-dev-tools.appspot.com/hit-builder/
    $data = array(
        'v' => 1,
        'tid' => 'UA-XXXXXX-Y', //***** Replace with your tracking ID!
        'cid' => ga_parse_cookie(),
        't' => 'event',
        'ec' => $category, //Category (Required)
        'ea' => $action, //Action (Required)
        'el' => $label, //Label
        'ev' => $value, //Value
        'ni' => $ni, //Non-Interaction
        'dh' => 'gearside.com', //Document Hostname
        'dp' => '/', //Document path
        'ua' => rawurlencode($_SERVER['HTTP_USER_AGENT']) //User Agent
    );
    ga_send_data($data);
}

然后,在您评论的位置,您只需放置函数:

ga_send_event('Form Button', 'Submit', 'Feedback');

关于php - 在 PHP 中添加 Google Analytics 事件以提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33834462/

相关文章:

php - 如何使用php显示mysql数据库中的图像?

php - 带有 'now() + INTERVAL' 的 INSERT 语句在具有命名占位符的 PDO 中插入 000

php - SimpleSAMLphp - SSO Azure 适用于 Firefox 和 Safari,但不适用于 Chrome 和 Edge

php - 如何在 PHP 中修剪 heredoc(长字符串)中的每一行

firebase - 用于TVOS的Firebase Analytics

sql - 了解是什么导致GBQ中的“资源超出”错误?

google-analytics - 使用 Google Analytics 自定义变量进行 A/B 测试

javascript - 将所有状态值传递给子组件

javascript - 在 Adob​​e Acrobat X Pro 中,如何使用 javascript 预填充 acrobat pdf 表单中的文本字段?

javascript - 我想使用 Jquery 或 Javascript 借助 for 循环和 ASC 顺序验证多个图像