摘要:進(jìn)行數(shù)據(jù)上報(bào)的時(shí)候,經(jīng)常會(huì)遇到列表數(shù)據(jù)曝光上報(bào)的問題,只對(duì)在當(dāng)前可視范圍內(nèi)的數(shù)據(jù)內(nèi)容進(jìn)行曝光上報(bào),而對(duì)于未在可視范圍內(nèi)的數(shù)據(jù)不進(jìn)行曝光上報(bào),等待用戶滾動(dòng)頁(yè)面或者區(qū)域使元素出現(xiàn)在可視范圍內(nèi)時(shí)才進(jìn)行曝光上報(bào)。
進(jìn)行數(shù)據(jù)上報(bào)的時(shí)候,經(jīng)常會(huì)遇到列表數(shù)據(jù)曝光上報(bào)的問題,只對(duì)在當(dāng)前可視范圍內(nèi)的數(shù)據(jù)內(nèi)容進(jìn)行曝光上報(bào),而對(duì)于未在可視范圍內(nèi)的數(shù)據(jù)不進(jìn)行曝光上報(bào),等待用戶滾動(dòng)頁(yè)面或者區(qū)域使元素出現(xiàn)在可視范圍內(nèi)時(shí)才進(jìn)行曝光上報(bào)。
解決方案目前針對(duì)此類問題,主要有兩種解決方案。
方案一:監(jiān)聽頁(yè)面或者區(qū)域scroll事件,通過getBoundingClientRect接口取元素的位置與可視窗口進(jìn)行判斷。function isElementInViewport(el) { var rect = el.getBoundingClientRect(); var width_st = rect.width / 2, height_st = rect.height / 2; var innerHeight = window.innerHeight, innerWidth = window.innerWidth; if ( rect.top <=0 && rect.height > innerHeight || rect.left <= 0 && rect.width > innerWidth ) { return rect.left * rect.right <= 0 || rect.top * rect.bottom <= 0 } return ( rect.height > 0 && rect.width > 0 && ( ( rect.top >= 0 && rect.top <= innerHeight - height_st ) || ( rect.bottom >= height_st && rect.bottom <= innerHeight ) ) && ( ( rect.left >= 0 && rect.left <= innerWidth - width_st ) || ( rect.right >= width_st && rect.right <= innerWidth ) ) ); } var nodes = document.querySelectorAll(".item") function report(node) { // 上報(bào)的邏輯 } window.onscroll = function() { nodes.forEach(node => { if( isElementInViewport(node) ) { report(node) } }) }
優(yōu)點(diǎn):兼容性好
缺點(diǎn):
需要關(guān)注頁(yè)面或者區(qū)域的scroll事件
頻繁的scroll事件,性能問題
方案二:通過 IntersectionObserver 監(jiān)聽元素是否處于可視范圍function report(node) { // 上報(bào)的邏輯 } var intersectionObserver = new IntersectionObserver(entries => { entries.forEach(entry => { if( entry.intersectionRatio > 0 ) { report(entry.target) } }) }) var nodes = document.querySelectorAll(".item") nodes.forEach(node => { intersectionObserver.observe(node) })
優(yōu)點(diǎn):
無須關(guān)注 scroll
回調(diào)是異步觸發(fā),不會(huì)頻繁觸發(fā),性能好
缺點(diǎn):兼容性不好?
實(shí)際上,針對(duì)兼容性問題,w3c 官方提供了對(duì)應(yīng) polyfill, 因此intersectionObserver用于生產(chǎn)是可行的。
總結(jié)筆者在實(shí)際運(yùn)用中,通過 IntersectionObserver 封裝了一個(gè)簡(jiǎn)單的調(diào)用庫(kù),應(yīng)用于可視化埋點(diǎn) sdk 中,用于解決元素曝光問題,如下
require("intersection-observer"); // polyfill class Exposure { constructor(callback) { if (!callback || typeof callback !== "function") { throw new Error("need callback or selector param") return } this.intersectionObserver = new IntersectionObserver((entries) => { entries.forEach(item => { if (item.intersectionRatio > 0) { if (item.target) { callback(item.target, item) this.intersectionObserver.unobserve(item.target) } } }) }); } observe(selector, ignoreExposured) { if (!this.intersectionObserver || !selector) { return } let nodes = [] if( this.isDOM(selector) ) { // dom節(jié)點(diǎn) nodes = [selector] }else { // 選擇器 nodes = document.querySelectorAll(selector) } if (!nodes.length) { return } nodes.forEach(node => { if (!ignoreExposured && node.__wg__tracker__exposured__) { return } node.__wg__tracker__exposured__ = true // 開始觀察 this.intersectionObserver.observe( node ); }) } disconnect() { if (!this.intersectionObserver) { return } this.intersectionObserver.disconnect() } isDOM(obj) { if( !obj ) { return false } if( typeof HTMLElement === "object" ) { return obj instanceof HTMLElement } if( typeof obj === "object" && obj.nodeType === 1 && typeof obj.nodeName === "string" ) { return true } return false } } export default Exposure
調(diào)用方法:
function report() {} var exposurer = new Exposure((node) => { report(node) }) exposurer.observe(".item)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/106422.html
獲取用戶的交互習(xí)慣及喜好,進(jìn)一步提升轉(zhuǎn)化率,可以在之前的埋點(diǎn)方案實(shí)現(xiàn)中,都是在具體的按鈕或者圖片被點(diǎn)擊或者被曝光時(shí)主動(dòng)通過事件去上報(bào)埋點(diǎn)。但這種方法適合在埋點(diǎn)比較少時(shí)還行的項(xiàng)目,遇見項(xiàng)目中需要大量埋點(diǎn)時(shí),添加的代碼就太多了,就會(huì)埋點(diǎn)邏輯與業(yè)務(wù)邏輯的高耦合?! ∮纱诵枰獡Q種方式。我先給大家普及下埋點(diǎn)上報(bào)方式都有哪些? 手動(dòng)埋點(diǎn) 可視化埋點(diǎn) 無痕埋點(diǎn) 手動(dòng)埋點(diǎn),顧名思義就是純手動(dòng)寫代碼,調(diào)...
摘要:列表頁(yè)曝光埋點(diǎn)實(shí)現(xiàn)以商品為例要求商品一半以上出現(xiàn)在視窗中時(shí)上報(bào)該行的商品快速滑動(dòng)過去的商品不上報(bào)滑動(dòng)過程中如果一行商品一直未消失在視野中一半以上,不能重復(fù)上報(bào)滑出視野的商品,再次滑入視野時(shí)需要再次上報(bào)分析需要以下信息商品所在行的高度固定值商 列表頁(yè)曝光埋點(diǎn)實(shí)現(xiàn) 以商品為例 要求 商品一半以上出現(xiàn)在視窗中時(shí) 上報(bào)該行的商品 快速滑動(dòng)過去的商品不上報(bào) 滑動(dòng)過程中如果一行商品一直未消失在視...
摘要:列表頁(yè)曝光埋點(diǎn)實(shí)現(xiàn)以商品為例要求商品一半以上出現(xiàn)在視窗中時(shí)上報(bào)該行的商品快速滑動(dòng)過去的商品不上報(bào)滑動(dòng)過程中如果一行商品一直未消失在視野中一半以上,不能重復(fù)上報(bào)滑出視野的商品,再次滑入視野時(shí)需要再次上報(bào)分析需要以下信息商品所在行的高度固定值商 列表頁(yè)曝光埋點(diǎn)實(shí)現(xiàn) 以商品為例 要求 商品一半以上出現(xiàn)在視窗中時(shí) 上報(bào)該行的商品 快速滑動(dòng)過去的商品不上報(bào) 滑動(dòng)過程中如果一行商品一直未消失在視...
摘要:本文借助發(fā)布的數(shù)字營(yíng)銷趨勢(shì)報(bào)告,從廣告的發(fā)展趨勢(shì)和尚存問題講起,引出前端開發(fā)在未來應(yīng)該逐漸的所處的角色和應(yīng)該承擔(dān)的責(zé)任??偨Y(jié)本文描述了廣告在年的發(fā)展趨勢(shì)和當(dāng)前存在的問題,并在廣告的渲染,監(jiān)測(cè)和上報(bào)上闡述了前端可以努力的方向。 上個(gè)周寫個(gè)篇文章《一篇文章了解廣告全鏈路》,在其中我們討論了數(shù)字營(yíng)銷中廣告是如何售賣的,以及廣告的實(shí)時(shí)競(jìng)價(jià)和庫(kù)存算法。但是,其實(shí)上面那篇文章還沒有完結(jié),它還缺了一...
摘要:背景最近在工作中,遇到了頁(yè)面跳轉(zhuǎn)時(shí)點(diǎn)擊上報(bào)丟失的問題,突出表現(xiàn)在微信的上,上報(bào)后直接跳轉(zhuǎn)的失敗率達(dá)到了驚人的。對(duì)這三種失效的方案有興趣的可以看頁(yè)面跳轉(zhuǎn)時(shí),統(tǒng)計(jì)數(shù)據(jù)丟失問題探討把要跳轉(zhuǎn)的放到中,然后在下一個(gè)頁(yè)面上上報(bào)點(diǎn)擊。 背景 最近在工作中,遇到了頁(yè)面跳轉(zhuǎn)時(shí)點(diǎn)擊上報(bào)丟失的問題,突出表現(xiàn)在微信ios的webview上,上報(bào)后直接跳轉(zhuǎn)的失敗率達(dá)到了驚人的93%。喝口水壓壓驚,開始逐步分析問...
閱讀 866·2023-04-25 15:13
閱讀 1480·2021-11-22 12:03
閱讀 882·2021-11-19 09:40
閱讀 1984·2021-11-17 09:38
閱讀 1801·2021-11-08 13:18
閱讀 705·2021-09-02 15:15
閱讀 1815·2019-08-30 15:54
閱讀 2792·2019-08-30 11:12