Help us understand the problem. What is going on with this article?

JimdoでGoogle Tag Managerのコードを挿入

More than 1 year has passed since last update.

Googleタグマネージャーでは、コンテナを作成したあとのインストールのために、2つのコードをサイトに埋め込む必要がある。1つはヘッダーなので特に問題ないが、2つめのコードはBody要素のなるべく先頭に入れるのが望ましいらしい。
Jimdoでやるのはちょいと困るが、動的に入れても動くらしい。

参考 JimdoサイトにGoogleタグマネージャーのコードを挿入する - idew.

上記記事はjQueryが使われていたので、jQueryを使わず、なおかつ1つ目のコードのまとめてヘッダーで挿入することにした。
GTX-XXXXX を置き換えてお使いください。

<script type="text/javascript">
//<![CDATA[
// Google Tag Manager
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
var google_tag_manager = '<iframe src="https://www.googletagmanager.com/ns.html?id=' + i + '" height="0" width="0" style="display:none;visibility:hidden"><\/iframe>';
    document.addEventListener("DOMContentLoaded", function() {
        var parent = d.body;
        var firstElement = parent.firstChildren;
        var element = d.createElement('noscript');
        element.innerHTML = google_tag_manager;
        parent.insertBefore(element, firstElement);
    });
})(window,document,'script','dataLayer','GTM-XXXXX');
// ]]>
</script>

参考

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした