GA4中如何建立Session时长转化指标?
从旧版GA过来的用户可能记得在GA中我们可以将访问时长超过一定秒数的事件标记为转化。但是GA4中并没有为我们提供这一定义。那么我们如何利用GTM和GA4来获得用户访问会话时长转化呢?马老师本篇将详细介绍步骤。

本篇大多数内容都来自Nico Brooks的教程。
大致思路和做法
因为HTTP协议的特性,我们会基于Cookie来写入用户开始会话的时间,并在一些关键的节点比较当前时间和开始时间之差来获取访问时长。当满足时长阈值后触发事件并推送到DataLayer,再推送给GA4。最后在GA4中识别该事件并将该事件定义为转化事件。
通过Cookie获得转化触发条件
我们先在GTM中建立一个Custom HTML Tag。代码如下:
<script>
/*
set the threshold for the session_duration_conv event here (in seconds)
if a session passes this threshold, a session_duration_conv event will
be pushed to the datalayer
*/
var sessionDurationThreshold = {{duration-threshold}};
function gtm_getCookie(name) {
var cookies = decodeURIComponent(document.cookie);
var cookiesArray = cookies.split(';');
var cookie = cookiesArray.find(function (item) {
return name == item.trim().substring(0, name.length);
});
return cookie;
}
(function() {
var startCookie = gtm_getCookie("gtm_session_start");
var thresholdCookie = gtm_getCookie("gtm_session_threshold");
var d1 = new Date();
// The getTime() method returns the number of milliseconds from
//midnight of January 1, 1970 to the specified date
var now = d1.getTime();
// convert the threshold to milliseconds
var durationThreshold = sessionDurationThreshold*1000-1;
var cookieExpiry = new Date();
// Set expiry time of 30 mins (in milliseconds)
cookieExpiry.setTime(cookieExpiry.getTime() + 30 * 60 * 1000);
var expires = "expires=" + cookieExpiry.toUTCString();
// if no session start cookie exists, create it and be done
if (!startCookie) {
document.cookie = "gtm_session_start" + "=" + now + ";" + expires + ";path=/";
} else if (!thresholdCookie){
// get the start time - the slice removes the name of the cookie
var startTime = startCookie.trim().slice(18);
var elapsedTime = now - startTime;
if (elapsedTime > durationThreshold) {
window.dataLayer = window.dataLayer || [];
dataLayer.push({ 'event': 'session_duration_conv' });
// once the event has been sent, set a threshold cookie so we
// don't send it again
document.cookie = "gtm_session_threshold=true;" + expires + ";path=/";
}
}
})();
</script>
此处我们用了一个变量duration-threshold。这我们可以在GTM中新建一个Constant变量,赋值为120。意为120秒。只要小于我们定义的30分钟Cookie失效的时长即可。
当访问时长超过120秒时,我们将推送session_duration_conv事件到dataLayer。
这个Custom HTML标签我们可以加上几个触发条件:
Scroll Depth可以设为滚动到页面25%,50%,75%,100%的时候触发。
这样在这些节点我们的JS脚本就会看是否比较Cookie的会话开始的时间戳后能够触发session_duration_conv事件。
添加GA4事件标签
这个步骤中我们要新建一个叫做Session Duration Trigger的Custom Event Trigger,它就是去监测我们dataLayer中是否出现session_duration_conv事件。

这样我们在GTM中的全部工作就完成了。总结一下,我们做了下面的改动
发布后,回到GA4。
在GA4中识别转化事件
一天后我们到GA4的Admin的Data Display中找到Events,如果我们找到了deep_engagement,那么我们只要把它标记为Conversion就行了。
另一种方法是同样在Data Display中找到Conversions,手动添加deep_engagement并且把Counting Method改为Once per session。如此便大功告成。
至此我们介绍了GA4中建立Session Duration转化的全过程。你学会了吗?如果有没明白的地方,欢迎通过评论反馈。