初めての訪問時はcookieにid・unixtimeを発行し2回目以降はunixtimeのみ更新

var cook;
var cStart,cEnd;
var cnt;

var unix = Math.floor((new Date()).getTime()/1E3);

if(navigator.cookieEnabled)
{
	cook=document.cookie + ";";
	console.log(cook);

	cStart = cook.indexOf("_na=",0);
	if(cStart == -1){   
		document.write("1回目の訪問です!");

		var char = "abcdefghijklmnopqrstuvwxyz123456789";
		var id = "";
		for(var i=0; i<10; i++){
			id += char[Math.floor(Math.random()*35)];
		}
		document.cookie="_na=NA."+id+"."+unix+";";
	} else {
		cEnd = cook.indexOf(";",cStart);
		cnt = cook.substring(cStart,cEnd - 10); // cStart + "_na="
		try {
			document.write(cnt);
			document.cookie=cnt+unix+";";
		} catch(e){
			document.write("訪問回数の取得に失敗しました。");
		}
	}
}else{
	document.write("cookieが使用できません。");
}

key:_na
value:NA.5ub8j1xn2p.1523632591

あれ、ブラウザを更新しても、GAのunixtimeが更新されない。。。何故だ????

cookieから何回目の訪問か判定する

<script>
var cook;
var cStart,cEnd;
var cnt;

if(navigator.cookieEnabled)
{
	cook=document.cookie + ";";
	console.log(cook);

	cStart = cook.indexOf("counts=",0);

	if(cStart == -1){   //indexOfで存在しない時は-1を返す
		document.write("1回目の訪問です!");
		document.cookie="counts=1;";
	} else {
		cEnd = cook.indexOf(";",cStart);
		cnt = cook.substring(cStart+7,cEnd); // cStart + "counts="
		try {
			cnt = parseInt(cnt)+1;
			document.write(cnt+"回目の訪問です!");

			document.cookie="counts="+cnt+";";
		} catch(e){
			document.write("訪問回数の取得に失敗しました。");
		}
	}
}else{
	document.write("cookieが使用できません。");
}

</script>

ga.jsのcookieの処理 18箇所出てきます。
出てくるのは、
_setCookiePath、_setVisitorCookieTimeout、_setSessionCookieTimeout、_setCampaignCookieTimeout、_cookiePathCopy、_setCookieTimeout、_setCookiePersistence
後半の、J.cookieは、J=documentなので、普通にdocument.cookieです。

b=n.cookie.split(";");

var Mc=N(),Nc=N(),Yb=N(),Jc=N(),Kc=N(),Lc=Va("utmtCookieName"),Cd=Va("displayFeatures"),Oc=N(),of=Va("gtmid"),Oe=Va("uaName"),Pe=Va("uaDomain"),Qe=Va("uaPath"),pf=Va("linkid"),w=N(),x=N(),y=N(),z=N();

V("_setCookiePath",P,9,0);

V("_setVisitorCookieTimeout",cb,28,1);V("_setSessionCookieTimeout",db,26,1);V("_setCampaignCookieTimeout",eb,29,1);

a("_cookiePathCopy",U.prototype.ha,30);

V("_setCookieTimeout",eb,25,3);V("_setCookiePersistence",cb,24,1);

pd=function(a){
	var b=[],c=J.cookie.split(";");
	a=new RegExp("^\\s*"+a+"=\\s*(.*?)\\s*$");
	for(var d=0;d

cookieのvalueは、"GA1.1.200404109.1517112693"とした場合、Timeoutの処理はUNIX timeで処理していそうですね。
あ、J.domain は、document.domainなので、refererのdomainが一緒だった場合は、UNIX Timeを引けば、滞在時間を求められますね。 なるほど、そういうことか。めちゃくちゃ頭いいな~、Paul Muret。閲覧開始数、直帰率は、リファラを見れば、判別できますね。
あれ、でも、同じドメインに遷移の場合はunix timeもsubstructすれば滞在時間が取得できますが、他のドメインに行ったり、ブラウザを閉じた場合、どうやってページ滞在時間を取得しているんでしょうか? 
なに~、わからん

あれ、こんな記載が。。。
>最後に訪問したページの滞在時間はGoogleアナリティクスが測定できないという注意点があるので気を付けましょう。
なるほど、やっぱりそうか!なぜ、cookieにunixtime入れてるのか完全に理解した!

ユーザー固有の ID:200404109 は何でしょうか?
あれ、もしかして、このユーザー(ID:200404109)が1回のセッションでどういうページ遷移をしたかを判別しているために発行しているのでは?
おお、少し仕組みが判ってきた~

cookieの書き込み

phpはsetcookie, jsはdocument.cookie

<?php
setcookie("key1", "value1")
?>
<script>
	document.cookie = 'key2=value2';
</script>

有効期限を設定

<script>
	var key = 'fsa';
	var expire = new Date();
	expire.setTime( expire.getTime() + 1000 * 3600 * 24 * 7);
	document.cookie = 'key=' + encodeURIComponent(key)+ '; path = /sample.php; expires = ' + expire.toUTCString();
</script>

ga.js line80-84

どうしよう、これ?このレベルまでもっていかないとお話にならないんですか。。。

var d=new Image(1,1);
d.src=c+a;
d.onload=function(){
	d.onload=null;
	d.onerror=null;
	b()};
d.onerror=function(){
	d.onload=null;
	d.onerror=null;
	b()}},
ef=function(a,b){
	if(0!=Ne().indexOf(J.location.protocol))return!1;
var c=W.XDomainRequest;
if(!c)return!1;
c=new c;
c.open("POST",Ne()+"/p/__utm.gif");
c.onerror=function(){b()};
c.onload=b;
c.send(a);
return!0},df=function(a,b,c,d){
	var e=W.XMLHttpRequest;if(!e)return!1;
	var f=new e;
	if(!("withCredentials"in f))return!1;
	f.open("POST",c||Ne()+"/p/__utm.gif",!0);
	f.withCredentials=
!0;
f.setRequestHeader("Content-Type","text/plain");
f.onreadystatechange=function(){
	if(4==f.readyState){
		if(d)try{var a=f.responseText;
			if(1>a.length||"1"!=a.charAt(0))Ra("xhr","ver",a),b();else if(3<d.count++)Ra("xhr","tmr",""+d.count),b();
			else if(1==a.length)b();
			else{var c=a.charAt(1);
				if("d"==c){var e=d.gb;
					a=(a=b)||Fa;
					df("",a,"https://stats.g.doubleclick.net/j/collect?"+e,d)}
					else if("g"==c){var t="https://www.google.%/ads/ga-audiences?".replace("%","com");
					gf(d.google,b,t);
					var Za=a.substring(2);
					if(Za)if(/^&#91;a-z.&#93;{1,6}$/.test(Za)){var Ma=
"https://www.google.%/ads/ga-audiences?".replace("%",Za);gf(d.google,Fa,Ma)}else Ra("tld","bcc",Za)}else Ra("xhr","brc",c),b()}}catch(mb){b()}else b();
f=null}};
f.send(a);
return!0},Ee=function(a,b){if(!J.body)return We(function(){Ee(a,b)},100),!0;
a=encodeURIComponent(a);
try{
	var c=J.createElement('<iframe name="'+a+'"></iframe>')}catch(e){c=J.createElement("iframe"),c.name=a}c.height="0";
	c.width="0";
	c.style.display="none";
	c.style.visibility="hidden";
	var d=Ne()+"/u/post_iframe.html";
	Ga(W,"beforeunload",
function(){
	c.src="";
	c.parentNode&&c.parentNode.removeChild(c)});
setTimeout(b,1E3);
J.body.appendChild(c);
c.src=d;return!0};
var qf=function(){
	this.G=this.w=!1;0==Ea()%1E4&&(H(142),this.G=!0);
	this.C={};
	this.D=[];
	this.U=0;
	this.S=[["www.google-analytics.com","","/plugins/"]];
	this._gasoCPath=this._gasoDomain=this.bb=void 0;Re();Se()};
	E=qf.prototype;E.oa=function(a,b){return this.hb(a,void 0,b)};
	E.hb=function(a,b,c){b&&H(23);c&&H(67);void 0==b&&(b="~"+M.U++);
	a=new U(b,a,c);
	M.C[b]=a;
	M.D.push(a);
	return a};
	E.u=function(a){
		a=a||"";
		return M.C[a]||M.hb(void 0,a)};
	E.pa=function(){
		return M.D.slice(0)};
		E.ab=function(){
			return M.D.length};
E.aa=function(){this.w=!0};
E.la=function(){this.G=!0};
var Fe=function(a){
	if("prerender"==J.visibilityState)return!1;
	a();
	return!0};
	var M=new qf;
	var D=W._gat;
	D&&Ba(D._getTracker)?M=D:W._gat=M;
	var Z=new Y;
	(function(a){if(!Fe(a)){H(123);
		var b=!1,c=function(){if(!b&&Fe(a)){b=!0;
			var d=J,e=c;
			d.removeEventListener?d.removeEventListener("visibilitychange",e,!1):d.detachEvent&&d.detachEvent("onvisibilitychange",e)}};
			Ga(J,"visibilitychange",c)}})(function(){var a=W._gaq,b=!1;
				if(a&&Ba(a.push)&&(b="[object Array]"==Object.prototype.toString.call(Object(a)),!b)){Z=a;
					return}W._gaq=Z;b&&Z.push.apply(Z,a)});
			function Yc(a){var b=1,c;
				if(a)for(b=0,c=a.length-1;0<=c;c--){var d=a.charCodeAt(c);
					b=(b<<6&268435455)+d+(d<<14);
					d=b&266338304;b=0!=d?b^d>>21:b}return b};
				}).call(this);

ga.js line71-79

E.initData=function(){
this.a.load()};
E.Ba=function(a){
a&&””!=a&&(this.set(Tb,a),this.a.j(“var”))};
var ne=function(a){
“trans”!==a.get(sc)&&500<=a.b(cc,0)&&a.stopPropagation(); if("event"===a.get(sc)){var b=(new Date).getTime(),c=a.b(dc,0),d=a.b(Zb,0); c=Math.floor((b-(c!=d?c:1E3*c))/1E3*1); 0=a.b(R,0)&&a.stopPropagation()}},pe=function(a){“event”===a.get(sc)&&a.set(R,Math.max(0,a.b(R,10)-1))};
var qe=function(){var a=[];
this.add=function(b,c,d){d&&(c=G(“”+c));
a.push(b+”=”+c)};
this.toString=function(){
return a.join(“&”)}},re=function(a,b){(b||2!=a.get(xb))&&a.Za(cc)},se=function(a,b){b.add(“utmwv”,”5.7.1″);
b.add(“utms”,a.get(cc));
b.add(“utmn”,Ea());
var c=J.location.hostname;
F(c)||b.add(“utmhn”,c,!0);
a=a.get(vb);
100!=a&&b.add(“utmsp”,a,!0)},te=function(a,b){b.add(“utmht”,(new Date).getTime());
b.add(“utmac”,Da(a.get(Wa)));
a.get(Oc)&&b.add(“utmxkey”,a.get(Oc),!0);a.get(vc)&&b.add(“utmni”,1);
a.get(of)&&b.add(“utmgtm”,a.get(of),!0);
var c=a.get(Ic);
c&&0=a.length)gf(a,b,c);
else if(8192>=a.length){if(0<=W.navigator.userAgent.indexOf("Firefox")&&![].reduce)throw new De(a.length); df(a,b)||ef(a,b)||Ee(a,b)||b()}else throw new Ce(a.length); },gf=function(a,b,c){c=c||Ne()+"/__utm.gif?"; [/code]

gs.js line63-70

E.Fa=function(a){a&&Ca(a)?(H(13),this.set(Hb,a,!0)):"object"===typeof a&&null!==a&&this.La(a);
this.H=a=this.get(Hb);
this.a.j("page");
this.K(a)};
E.F=function(a,b,c,d,e){if(""==a||!wd(a)||""==b||!wd(b)||void 0!=c&&!wd(c)||void 0!=d&&!xd(d))return!1;
this.set(wc,a,!0);
this.set(xc,b,!0);
this.set(yc,c,!0);
this.set(zc,d,!0);
this.set(vc,!!e,!0);
this.a.j("event");
return!0};
E.Ha=function(a,b,c,d,e){
	var f=this.a.b(Dc,0);
	1*e===e&&(f=e);
	if(this.a.b(Q,0)%100>=f)
		return!1;
	c=1*(""+c);if(""==a||!wd(a)||""==b||!wd(b)||!xd(c)||isNaN(c)||0>c||0>f||100<f||void 0!=d&&(""==d||!wd(d)))return!1;
	this.ib(me(a,b,c,d));return!0};
	E.Ga=function(a,b,c,d){if(!a||!b)return!1;
		this.set(Ac,a,!0);
		this.set(Bc,b,!0);
		this.set(Cc,c||J.location.href,!0);
		d&&this.set(Hb,d,!0);
		this.a.j("social");
		return!0};
		E.Ea=function(){this.set(Dc,10);
			this.K(this.H)};
			E.Ia=function(){this.a.j("trans")};
E.ib=function(a){this.set(Eb,a,!0);
	this.a.j("event")};
	E.ia=function(a){this.initData();
		var b=this;
		return{_trackEvent:function(c,d,e){H(91);b.F(a,c,d,e)}}};
		E.ma=function(a){return this.get(a)};
		E.xa=function(a,b){if(a)if(Ca(a))this.set(a,b);
			else if("object"==typeof a)for(var c in a)a.hasOwnProperty(c)&&this.set(c,a&#91;c&#93;)};
			E.addEventListener=function(a,b){(a=this.get(Nc)&#91;a&#93;)&&a.push(b)};
			E.removeEventListener=function(a,b){a=this.get(Nc)&#91;a&#93;;
				for(var c=0;a&&c<a.length;c++)if(a&#91;c&#93;==b){a.splice(c,1);break}};
E.qa=function(){return"5.7.1"};
E.B=function(a){this.get(hb);
	a="auto"==a?Ka(J.domain):a&&"-"!=a&&"none"!=a?a.toLowerCase():"";
	this.set(bb,a)};
	E.va=function(a){this.set(hb,!!a)};
	E.na=function(a,b){return ce(this.a,a,b)};
	E.link=function(a,b){this.a.get(fb)&&a&&(J.location.href=ce(this.a,a,b))};
	E.ua=function(a,b){this.a.get(fb)&&a&&a.action&&(a.action=ce(this.a,a.action,b))};
E.za=function(){this.initData();
	var a=this.a,b=J.getElementById?J.getElementById("utmtrans"):J.utmform&&J.utmform.utmtrans?J.utmform.utmtrans:null;
	if(b&&b.value){a.set(Cb,&#91;&#93;);
		b=b.value.split("UTM:");
		for(var c=0;c<b.length;c++){b&#91;c&#93;=Da(b&#91;c&#93;);
			for(var d=b&#91;c&#93;.split(de),e=0;e<d.length;e++)d&#91;e&#93;=Da(d&#91;e&#93;);
				"T"==d&#91;0&#93;?fe(a,d&#91;1&#93;,d&#91;2&#93;,d&#91;3&#93;,d&#91;4&#93;,d&#91;5&#93;,d&#91;6&#93;,d&#91;7&#93;,d&#91;8&#93;):"I"==d&#91;0&#93;&&ge(a,d&#91;1&#93;,d&#91;2&#93;,d&#91;3&#93;,d&#91;4&#93;,d&#91;5&#93;,d&#91;6&#93;)}}};
			E.$=function(a,b,c,d,e,f,Be,k){return fe(this.a,a,b,c,d,e,f,Be,k)};
E.Y=function(a,b,c,d,e,f){
	return ge(this.a,a,b,c,d,e,f)};
	E.Aa=function(a){de=a||"|"};
	E.ea=function(){this.set(Cb,&#91;&#93;)};
	E.wa=function(a,b,c,d){var e=this.a;if(0>=a||a>e.get(yb))a=!1;
		else if(!b||!c||128<b.length+c.length)a=!1;
		else{1!=d&&2!=d&&(d=3);
			var f={};
			f.name=b;f.value=c;f.scope=d;e.get(Fb)&#91;a&#93;=f;a=!0}a&&this.a.store();
			return a};
			E.ka=function(a){this.a.get(Fb)&#91;a&#93;=void 0;this.a.store()};E.ra=function(a){
				return(a=this.a.get(Fb)&#91;a&#93;)&&1==a.scope?a.value:void 0};
E.Ca=function(a,b,c){12==a&&1==b?this.set(pf,c):this.m().f(a,b,c)};
E.Da=function(a,b,c){this.m().o(a,b,c)};
E.sa=function(a,b){return this.m().getKey(a,b)};
E.ta=function(a,b){return this.m().N(a,b)};
E.fa=function(a){this.m().L(a)};
E.ga=function(a){this.m().M(a)};
E.ja=function(){return new yd};
E.W=function(a){a&&this.get(Ab).push(a.toLowerCase())};
E.ba=function(){this.set(Ab,&#91;&#93;)};
E.X=function(a){a&&this.get(Bb).push(a.toLowerCase())};
E.ca=function(){this.set(Bb,&#91;&#93;)};
E.Z=function(a,b,c,d,e){if(a&&b){a=&#91;a,b.toLowerCase()&#93;.join(":");
if(d||e)a=&#91;a,d,e&#93;.join(":");
d=this.get(zb);
d.splice(c?0:d.length,0,a)}};
E.da=function(){this.set(zb,&#91;&#93;)};
E.ha=function(a){this.a.load();
	var b=this.get(P),c=be(this.a);
	this.set(P,a);
	this.a.store();
	ae(this.a,c);
	this.set(P,b)};
E.ya=function(a,b){
	if(0<a&&5>=a&&Ca(b)&&""!=b){
		var c=this.get(Fc)||[];
		c[a]=b;
		this.set(Fc,c)}};
E.V=function(a){
	a=""+a;if(a.match(/^[A-Za-z0-9]{1,5}$/)){
		var b=this.get(Ic)||[];
		b.push(a);this.set(Ic,b)}};

ga.js line55-62

ここらへんで、判定した配列を配列にsetしてますね。

a.responseEnd-a.responseStart,a.fetchStart-b,a.domInteractive-b,a.domContentLoadedEventStart-b]}},$e=function(){
if(W.top==W){
var a=W.external,b=a&&a.onloadT;
a&&!a.isValidLoadTime&&(b=void 0);
2147483648b.length)){for(var c=[],d=0;d

ga.js line49-54

なんか出てきた。
https://www.google.com/analytics/web/inpage/pub/inpage.js

jf=/^https?:\/\/(r\.)?search\.yahoo\.com?(\.jp)?\/?[^?]*$/i,rf=/^https?:\/\/(www\.)?bing\.com\/?$/i,Nd=function(a){a=Pa(a.get(Jb),a.get(P));try{if(Td.test(a))return H(136),a+”?q=”;if(jf.test(a))return H(150),a+”?p=(not provided)”;if(rf.test(a))return a+”?q=(not provided)”}catch(b){H(145)}return a};var Ud,Vd,Wd=function(a){Ud=a.c(S,””);Vd=a.c(kc,””)},Xd=function(a){var b=a.c(S,””),c=a.c(kc,””);b!=Ud&&(-1e?b+”#”+d:b+”&”+d;
c=””;
0b.indexOf(“?”)?b+”?”+d+c:b+”&”+d+c},$d=function(a,b,c,d){for(var e=0;3>e;e++){for(var f=
0;3>f;f++){if(d==Yc(a+b+c))return H(127),[b,c];
var Be=b.replace(/ /g,”%20″),k=c.replace(/ /g,”%20″);
if(d==Yc(a+Be+k))return H(128),[Be,k];Be=Be.replace(/\+/g,”%20″);
k=k.replace(/\+/g,”%20″);
if(d==Yc(a+Be+k))return H(129),[Be,k];
try{var Ja=b.match(“utmctr=(.*?)(?:\\|utm|$)”);
if(Ja&&2==Ja.length&&(Be=b.replace(Ja[1],G(I(Ja[1]))),d==Yc(a+Be+c)))
return H(139),[Be,c]}catch(t){}b=I(b)}c=I(c)}};
var de=”|”,fe=function(a,b,c,d,e,f,Be,k,Ja){var t=ee(a,b);
t||(t={},a.get(Cb).push(t));t.id_=b;t.affiliation_=c;t.total_=d;t.tax_=e;t.shipping_=f;t.city_=Be;t.state_=k;t.country_=Ja;t.items_=t.items_||[];return t},ge=function(a,b,c,d,e,f,Be){a=ee(a,b)||fe(a,b,””,0,0,0,””,””,””);
a:{if(a&&a.items_){var k=a.items_;for(var Ja=0;Jab.length||!/^\d+$/.test(b[0])||(b[0]=””+c,Fd(a,”__utmx”,b.join(“.”),void 0))},be=function(a,b){a=$c(a.get(O),pd(“__utmx”));
“-“==a&&(a=””);
return b?G(a):a},Ye=function(a){try{var b=La(J.location.href,!1),c=decodeURIComponent(L(b.R.get(“utm_referrer”)))||””;
c&&a.set(Jb,c);
var d=decodeURIComponent(K(b.R.get(“utm_expid”)))||””;
d&&(d=d.split(“.”)[0],a.set(Oc,””+d))}catch(e){H(146)}},l=function(a){var b=W.gaData&&W.gaData.expId;b&&a.set(Oc,
“”+b)};
var ke=function(a,b){var c=Math.min(a.b(Dc,0),100);
if(a.b(Q,0)%100>=c)return!1;
c=Ze()||$e();
if(void 0==c)return!1;var d=c[0];
if(void 0==d||Infinity==d||isNaN(d))return!1;
0a[b])return!1;
return!0},le=function(a){return isNaN(a)||0>a?0:5E3>a?10*Math.floor(a/10):5E4>a?100*Math.floor(a/100):41E5>a?1E3*Math.floor(a/1E3):41E5},je=function(a){
for(var b=new yd,c=0;c

ga.js line44-48

なるほど、get parameterを配列に入れて保存してるんだ。

Sd=function(a){
if(a.get(kb)&&!a.get(Mc)){var b=!F(a.get(ic))||!F(a.get(nc))||!F(a.get(S))||!F(a.get(lc));
for(var c={},d=0;d