3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

google analycs4 (ga4) の遅延読み込み

Posted at

なんとなくいけてる気がしますので、掲載してみます。

lazy-gtag.js
var MEASUREMENT_ID="G-xxxxxxxx";

$(function() {
	setgtag();
});

function setgtag() {
	setTimeout(function() {
		var gtc = document.createElement("script");
		gtc.innerHTML='window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag("js",new Date());gtag("config", MEASUREMENT_ID);';

		// 下記はデバッグモード用
		//gtc.innerHTML='window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag("js",new Date());gtag("config", MEASUREMENT_ID,{"debug_mode":true});';
		document.body.appendChild(gtc);

		var gt=document.createElement("script");
		gt.src='https://www.googletagmanager.com/gtag/js?id=' + MEASUREMENT_ID;
		document.body.appendChild(gt);

	}, 2500);
}

一応デバッガーでもOKが出てます

image.png

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?