一个简单的代码段,用于跟踪网站上的网页浏览量,而无需添加外部库。 也适用于使用react和vue.js等单页应用程序。

Before

Google Tag Manager + Analytics = 73kB

After

Snippet = 1.5kB

<script>
(function(a,b,c){var d=a.history,e=document,f=navigator||{},g=localStorage,
h=encodeURIComponent,i=d.pushState,k=function(){return Math.random().toString(36)},
l=function(){return g.cid||(g.cid=k()),g.cid},m=function(r){var s=[];for(var t in r)
r.hasOwnProperty(t)&&void 0!==r[t]&&s.push(h(t)+"="+h(r[t]));return s.join("&")},
n=function(r,s,t,u,v,w,x){var z="https://www.google-analytics.com/collect",
A=m({v:"1",ds:"web",aip:c.anonymizeIp?1:void 0,tid:b,cid:l(),t:r||"pageview",
sd:c.colorDepth&&screen.colorDepth?screen.colorDepth+"-bits":void 0,dr:e.referrer||
void 0,dt:e.title,dl:e.location.origin+e.location.pathname+e.location.search,ul:c.language?
(f.language||"").toLowerCase():void 0,de:c.characterSet?e.characterSet:void 0,
sr:c.screenSize?(a.screen||{}).width+"x"+(a.screen||{}).height:void 0,vp:c.screenSize&&
a.visualViewport?(a.visualViewport||{}).width+"x"+(a.visualViewport||{}).height:void 0,
ec:s||void 0,ea:t||void 0,el:u||void 0,ev:v||void 0,exd:w||void 0,exf:"undefined"!=typeof x&&
!1==!!x?0:void 0});if(f.sendBeacon)f.sendBeacon(z,A);else{var y=new XMLHttpRequest;
y.open("POST",z,!0),y.send(A)}};d.pushState=function(r){return"function"==typeof d.onpushstate&&
d.onpushstate({state:r}),setTimeout(n,c.delay||10),i.apply(d,arguments)},n(),
a.ma={trackEvent:function o(r,s,t,u){return n("event",r,s,t,u)},
trackException:function q(r,s){return n("exception",null,null,null,null,r,s)}}})
(window,"XX-XXXXXXXXX-X",{anonymizeIp:true,colorDepth:true,characterSet:true,screenSize:true,language:true});
</script> Setup: Just put the snippet into your html, replace 'XX-XXXXXXXXX-X' with your tracking id and you're ready to go. You can also add options for what information you want to track. Event: ma.trackEvent('Category', 'Action', 'Label', 'Value') Exception: ma.trackException('Description', 'Fatal')

最小化的 Google Analytics 代码的更多相关文章

  1. C# 实现WinForm窗口最小化到系统托盘代码,并且判断左右鼠标的事件

    1.设置WinForm窗体属性showinTask=false 2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标. 3.添加窗体最小 ...

  2. C# 实现WinForm窗口最小化到系统托盘代码

    1.如果不想让程序在任务栏中显示,请把窗体的属性ShowInTaskbar设置为false; 2.如果想让程序启动时就最小化,请设置窗体的属性WindowState设置为Minimized.(Mini ...

  3. Android下最小化程序到后台代码

    procedure TForm1.Button4Click(Sender: TObject); var    Intent: JIntent; begin    Intent := TJIntent. ...

  4. 【转载】国内网站博客数据统计选免费Google Analytics还是百度统计

    [转载]国内网站博客数据统计选免费Google Analytics还是百度统计 Google Analytics谷歌统计是我用的第一个网站统计工具,当然现在也一直在用.Google Analytics ...

  5. Java之路(六) 局部变量作用域最小化

    将局部变量的作用域最小化,可以增强代码的可读性和可维护性,并降低出错的可能性. 将局部变量的作用域最小化的方法有: 方法1:在第一次使用某个局部变量的地方进行声明. a.Java可以在任何可以出现语句 ...

  6. 在像Angular2这样的SPA应用中使用Google Analytics的方法

    Angular2のようなシングルページアプリケーションでGoogleアナリティクスを使う方法 如何在像Angular2这样的SPA应用中使用Google Analytics? 试着调查了一下. 由于S ...

  7. c++ win32下窗口的最小化到托盘以及还原

    框架是用visual studio创建的win32控制台模板,然后自己添加最小化及还原部分代码. 需要额外包含的头文件: #include <shellapi.h> #include &l ...

  8. Google Analytics统计代码GA.JS中文教程

    2010-12-06 11:07:08|  分类: java编程 |  标签:google  analytics  ga  js  代码  |举报|字号 订阅     Google Analytics ...

  9. delphi代码实现窗口最小化,最大化,关闭消息发送

      分类: 代码实现窗口最小化,最大化,关闭 var hwnd: hwnd;//句柄 PostMessage(hwnd,WM_SYSCOMMAND, SC_MINIMIZE,0); //最小化Post ...

随机推荐

  1. rem布局完成响应式开发,通俗且详细的原理解析和代码实现

    一.rem布局基本原理 原理:rem可以理解为一个长度单位,单位rem的值等于网页font-size的值.如果网页的字体大小为默认值16px,那么1rem就等于16px,0.5rem等于8px. 根据 ...

  2. Kubernetes — 我的第一个容器化应用

    而在这篇文章中,我们就来扮演一个应用开发者的角色,使用这个 Kubernetes 集群发布第一个容器化应用. 在开始实践之前,我先给你讲解一下 Kubernetes 里面与开发者关系最密切的几个概念. ...

  3. Kali学习笔记42:SQL手工注入(4)

    前三篇文章都是在讲发现SQL注入漏洞 如何查询得到所有的信息 那么另一条思路还未尝试过:能否修改数据? 例如这样: '; update users set user='yiqing' where us ...

  4. [Swift]LeetCode518. 零钱兑换 II | Coin Change 2

    You are given coins of different denominations and a total amount of money. Write a function to comp ...

  5. [Swift]LeetCode1025. 除数博弈 | Divisor Game

    Alice and Bob take turns playing a game, with Alice starting first. Initially, there is a number N o ...

  6. Java高级特性-String、StringBuffer和StringBuilder

    Java高级特性-String.StringBuffer和StringBuilder String Java语言中的字符串值都属于String类,虽然有其他方法表示字符串(如字符串数组),但Java一 ...

  7. MVC中的下载文件及上传

    前言:最近做的项目中用到了文件下载与上传,一下子想不起来,只能进行百度,为了方便自己做了一个小demo,特此写了这篇小笔记 1.页面方面: 2.控制器方面 namespace MvcUpload.Co ...

  8. bind(),unbind(),hover(),toggle(),animate()

    First.bind() bind(Type,fn) type:click/mouseover/mouseout fn:functionsecond.unbind() unbind(Type,fn) ...

  9. 关于ML.NET v0.6的发布说明

    ML.NET 0.6版本提供了几项令人兴奋的新增功能: 用于构建和使用机器学习模型的新API 我们主要关注的是发布用于构建和使用模型的新ML.NET API的第一次迭代.这些新的,更灵活的API支持新 ...

  10. 三种方式给apt设置代理

    为什么设置代理,你懂得. 有很多第三方工具可以用,比如proxychains,非常好用,不过今天这不是正题.因为有可能没有代理,上网你都做不到,更别提下载软件了.想一想方法还是告诉你,免得你万一必须用 ...