书中用到的代码

Google Analytics 4 Haran 7年前 (2017-10-10) 5944次浏览 3个评论
文章目录[隐藏]

2.5.2、数据导入代码

书中用到的代码

由于这段代码触发了服务器的安全机制,所以只能以截图的形式。

3.2.7、事件跟踪代码

function() {
	var elem = {{element}},
	    attr = "ga-data", // change this to the attribute that you want to get
        result = (elem.getAttribute && elem.getAttribute(attr)) || null;

	if( !result ) {
		var attrs = elem.attributes,
			l = attrs.length;
		for(var i = 0; i < l; i++) {
			if(attrs[i].nodeName === attr)
				result = attrs[i].nodeValue;
                }
	}

	return result;
}

3.2.10、获取用户IP代码

<script type="application/javascript">
   $.get("https://ipinfo.io", function(response) {
             console.log("My public IP address is: ", response.ip);
          
     var cookieName= "ipnum";
    var ipnumber  =response.ip;
    var cookiePath  = "/";
 
    
    var expirationTime = 172800*15;                           
    expirationTime = expirationTime * 1000;
 
    var date = new Date();
    var dateTimeNow = date.getTime();
    date.setTime(dateTimeNow + expirationTime);
 
    var expirationTime = date.toUTCString();
 
    document.cookie = cookieName+"="+ipnumber+"; expires="+expirationTime+"; path="+cookiePath;
        }, "json")
</script>



3.2.11、对按钮颜色做测试代码

https://github.com/thenextweb/cro

3.2.12、跟踪用户复制代码

<script>
// Declare function to get selected text from document
function getSelectionText() {
    var text = "";
    if (window.getSelection) {
        text = window.getSelection().toString();
    } else if (document.selection && document.selection.type != "Control") {
        text = document.selection.createRange().text;
    }
    return text;
}

// Declare function on copy event
document.addEventListener("copy", function(e){
  dataLayer.push({
    "event": "textCopied",
    "clipboardText": getSelectionText(),
    "clipboardLength": getSelectionText().length
  });
});
</script>

3.2.13、跟踪页面访问深度代码

<script>
!function(e,n,t){"use strict";var r,o,a,l,i={minHeight:0,elements:[],percentage:!0,userTiming:!1,pixelDepth:!1,nonInteraction:!0,gaGlobal:!1,gtmOverride:!1},c=e(n),u=[],g=0;e.scrollDepth=function(h){function p(e){l?l({event:"ScrollDistance",eventCategory:"Scroll Depth",eventAction:e,eventLabel:"Baseline",eventValue:1,eventNonInteraction:!0}):(r&&n[a]("send","event","Scroll Depth",e,"Baseline",1,{nonInteraction:!0}),o&&_gaq.push(["_trackEvent","Scroll Depth",e,"Baseline",1,!0]))}function s(e,t,i,c){l?(l({event:"ScrollDistance",eventCategory:"Scroll Depth",eventAction:e,eventLabel:t,eventValue:1,eventNonInteraction:h.nonInteraction}),h.pixelDepth&&arguments.length>2&&i>g&&(g=i,l({event:"ScrollDistance",eventCategory:"Scroll Depth",eventAction:"Pixel Depth",eventLabel:D(i),eventValue:1,eventNonInteraction:h.nonInteraction})),h.userTiming&&arguments.length>3&&l({event:"ScrollTiming",eventCategory:"Scroll Depth",eventAction:e,eventLabel:t,eventTiming:c})):(r&&(n[a]("send","event","Scroll Depth",e,t,1,{nonInteraction:h.nonInteraction}),h.pixelDepth&&arguments.length>2&&i>g&&(g=i,n[a]("send","event","Scroll Depth","Pixel Depth",D(i),1,{nonInteraction:h.nonInteraction})),h.userTiming&&arguments.length>3&&n[a]("send","timing","Scroll Depth",e,c,t)),o&&(_gaq.push(["_trackEvent","Scroll Depth",e,t,1,h.nonInteraction]),h.pixelDepth&&arguments.length>2&&i>g&&(g=i,_gaq.push(["_trackEvent","Scroll Depth","Pixel Depth",D(i),1,h.nonInteraction])),h.userTiming&&arguments.length>3&&_gaq.push(["_trackTiming","Scroll Depth",e,c,t,100])))}function v(e){return{"25%":parseInt(.25*e,10),"50%":parseInt(.5*e,10),"75%":parseInt(.75*e,10),"100%":e-5}}function f(n,t,r){e.each(n,function(n,o){-1===e.inArray(n,u)&&t>=o&&(s("Percentage",n,t,r),u.push(n))})}function m(n,t,r){e.each(n,function(n,o){-1===e.inArray(o,u)&&e(o).length&&t>=e(o).offset().top&&(s("Elements",o,t,r),u.push(o))})}function D(e){return(250*Math.floor(e/250)).toString()}function d(e,n){var t,r,o,a=null,l=0,i=function(){l=new Date,a=null,o=e.apply(t,r)};return function(){var c=new Date;l||(l=c);var u=n-(c-l);return t=this,r=arguments,0>=u?(clearTimeout(a),a=null,l=c,o=e.apply(t,r)):a||(a=setTimeout(i,u)),o}}var y=+new Date;h=e.extend({},i,h),e(t).height()<h.minHeight||(h.gaGlobal?(r=!0,a=h.gaGlobal):"function"==typeof ga?(r=!0,a="ga"):"function"==typeof __gaTracker&&(r=!0,a="__gaTracker"),"undefined"!=typeof _gaq&&"function"==typeof _gaq.push&&(o=!0),"function"==typeof h.eventHandler?l=h.eventHandler:"undefined"==typeof dataLayer||"function"!=typeof dataLayer.push||h.gtmOverride||(l=function(e){dataLayer.push(e)}),h.percentage?p("Percentage"):h.elements&&p("Elements"),c.on("scroll.scrollDepth",d(function(){var r=e(t).height(),o=n.innerHeight?n.innerHeight:c.height(),a=c.scrollTop()+o,l=v(r),i=+new Date-y;return u.length>=4+h.elements.length?void c.off("scroll.scrollDepth"):(h.elements&&m(h.elements,a,i),void(h.percentage&&f(l,a,i)))},500)))}}(jQuery,window,document);jQuery.scrollDepth();
</script>

跟踪用户是否打印页面

<script type="text/javascript">
(function () {
var runOnce; 
var afterPrint = function() {
    
    if (!runOnce) { // Because of Chrome we can only allow the code to run once.
        runOnce = true;
        dataLayer.push({'event': 'printPage'}); // Send Print Event to GTM
    };
};
if (window.matchMedia) { // Track printing from browsers using the Webkit engine
    var mediaQueryList = window.matchMedia('print');
    mediaQueryList.addListener(function(mql) {
        if (!mql.matches) {
            afterPrint();
        }
    });
}
window.onafterprint = afterPrint; // Internet Explorer
$(document).keydown(function(allBrowsers) { // Track printing using Ctrl/Cmd+P.
    if (allBrowsers.keyCode==80 && (allBrowsers.ctrlKey || allBrowsers.metaKey)) {
        if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. 
            afterPrint();
        }
    }
});
// Print activated using window.print() ex. from a button.
$('.printlink').click(function() {
    if ($.browser.opera) { // Opera is a little different so we must send the afterPrint() function to get the tracking to work. 
        afterPrint();
    }
    window.print(); // If window.print() is activated in another script, you can remove it from this tracking script.
});
}());
</script>

如有疑问,可以在文章底部留言或邮件(haran.huang@ichdata.com) 我~
喜欢 (2)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(3)个小伙伴在吐槽
  1. 怎么都没有了。。站长求补上!
    Laura2022-11-02 16:54 回复 Windows 10 | Chrome 86.0.4240.198
    • Haran
      Wordpress版本问题导致,已经升级解决
      黄业忠2022-11-02 17:59 回复 Mac OS X | Chrome 107.0.0.0
  2. 123
    GA小站2017-11-07 16:55 回复 Windows 10 | Chrome 61.0.3163.100