javascript - 在 Opencart 中使用用户 ID 实现 Google Analytics

标签 javascript php google-analytics opencart

我想跟踪每个客户,我该怎么做谷歌分析上的文档有这个代码

ga(‘set’, ‘&uid’, {{USER_ID}}); // Set the user ID using signed-in user_id.

我如何在其中传递用户ID,因为开放式购物车有一个管理面板设置来输入谷歌分析,如果我的客户没有登录,我应该做什么。

最佳答案

Opencart 使用管理中的一个设置,您可以在其中插入整个 GA 代码。您也可以使用它来进行用户跟踪。您要做的唯一更改是将这行 JS 代码添加到管理中的该字段中:

ga('set', '&uid', #CUSTOMER_ID#);

此字符串将由 PHP 在 catalog/controller/common/header.php 中解析,如下所示 - 找到这一行

$this->data['google_analytics'] = html_entity_decode( ... );

在此行之后放置以下代码:

if ($this->customer->isLogged()) {
    // replace %s with customer ID
    $this->data['google_analytics'] = str_replace('#CUSTOMER_ID#', $this->customer->getId(), $this->data['google_analytics']);
} else {
    // customer is not logged in, remove the user tracking part
    $this->data['google_analytics'] = str_replace("ga('set', '&uid', #CUSTOMER_ID#);", "", $this->data['google_analytics']);
}

这应该可以做到 - 还可以检查客户是否登录。

关于javascript - 在 Opencart 中使用用户 ID 实现 Google Analytics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27798822/

相关文章:

javascript - 从客户端脚本获取值并覆盖服务器端脚本

php - MySQL函数给出错误的值

php - 使用CRON导入远程.sql文件

python - collective.googleanalytics 报告中的自定义维度

google-analytics - Google Analytics(分析)中的事件标签和类别

javascript - 使用 d3.js 以 3x3 格式绘制多个圆圈

javascript - AngularJS ngAnimate 和 animate.css

javascript - 通过 webview 动态调整应用程序窗口(边界)的大小

javascript - 将数组从 JS 传递到 PHP 并创建 SESSION 变量

html - pingdom 正常运行时间监控器是否会增加我的 GA 跳出率