通过测量协议发送Session_ID

发布于 2025-02-11 00:32:53 字数 173 浏览 3 评论 0原文

我具有前端GA4实现,并希望通过MP从服务器发送交易数据(和一些自定义事件)。与Google提议的建议

将session_id作为参数,以便测量协议事件 出现在基于会话的报告

从gtag.js获得session_id或生成随机session_id?

I have front-end GA4 implementation and want to send transactions data (and some custom events) from the server via MP. With recomendations of Google to

include session_id as a param, so that measurement protocol events
appear in session-based reporting

is it better to get session_id from gtag.js or generate random session_id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

〗斷ホ乔殘χμё〖 2025-02-18 00:32:53

根据 smarityment>“分析” A>使用“ Session_ID”参数的随机值将启动新的会话。

根据“ nofollow noreferrer”>测量协议(Google Analyticals 4)/changelog '>'>'>'>'>'> Session_ID'需要在基于会话的报告中获取服务器端事件。

解决方案:

'会话ID'存储在cookie中,似乎是会话开始日期的时间戳。使用PHP在服务器端获取“会话ID”:

/**
 * Gets GA Session Id (GA4 only) from cookies.
 *
 * @var string $measurement_id
 *   GA4 Measurement Id (Property Id). E.g., 'G-1YS1VWHG3V'.
 *
 * @return int
 *   Returns GA4 Session Id or NULL if cookie wasn't found.
 */
function _get_browser_session_id($measurement_id) {
  // Cookie name example: '_ga_1YS1VWHG3V'.
  $cookie_name = '_ga_' . str_replace('G-', '', $measurement_id);
  if (isset($_COOKIE[$cookie_name])) {
    // Cookie value example: 'GS1.1.1659710029.4.1.1659710504.0'.
    // Session Id:                  ^^^^^^^^^^.
    $parts = explode('.', $_COOKIE[$cookie_name]);
    return $parts[2];
  }
}

According to Measurement Protocol (Google Analytics 4)/Sending events using random value for 'session_id' parameter will start new session.

According to Measurement Protocol (Google Analytics 4)/Changelog 'session_id' is required to get server-side events in session-based reports.

Solution:

'Session Id' is stored in cookies and seems it's a timestamp of session start date. To get 'Session Id' at server side using PHP:

/**
 * Gets GA Session Id (GA4 only) from cookies.
 *
 * @var string $measurement_id
 *   GA4 Measurement Id (Property Id). E.g., 'G-1YS1VWHG3V'.
 *
 * @return int
 *   Returns GA4 Session Id or NULL if cookie wasn't found.
 */
function _get_browser_session_id($measurement_id) {
  // Cookie name example: '_ga_1YS1VWHG3V'.
  $cookie_name = '_ga_' . str_replace('G-', '', $measurement_id);
  if (isset($_COOKIE[$cookie_name])) {
    // Cookie value example: 'GS1.1.1659710029.4.1.1659710504.0'.
    // Session Id:                  ^^^^^^^^^^.
    $parts = explode('.', $_COOKIE[$cookie_name]);
    return $parts[2];
  }
}
青衫负雪 2025-02-18 00:32:53

为了使GA4能够将事件归因于原始会话,您应该提供session_id。它可以通过gtag

gtag('get', 'G-XXXXXXXX', 'session_id', (sessionId) => {
  console.log(sessionId);
});

资源:

In order for GA4 to be able to attribute the event to the original session, you should provide the session_id. It can be retrieved through gtag:

gtag('get', 'G-XXXXXXXX', 'session_id', (sessionId) => {
  console.log(sessionId);
});

Resources:

往日 2025-02-18 00:32:53

我想我在这里找到了解决方案 https://www.optimiessmart.com/what-is-measurement-protocolotocol-in-google-in-google-analytics-4-ga4/#11-10-10-sessision-id-id--

请求GA的请求中的参数:

  1. “ CID” - 是“ GA CLIELT_ID”
  2. “ SID” - 这是Session_ID,您需要将其保存在附近的db“ cid”参数中,然后使用client_id一起使用测量协议发送它

I think I found the solution here https://www.optimizesmart.com/what-is-measurement-protocol-in-google-analytics-4-ga4/#11-10-session-id-

There 2 params in the request to GA:

  1. "cid" - it's "GA client_id"
  2. "sid" - this is session_id and you need to save it in your DB near "cid" param and then send it using measurement protocol along with client_id
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文