【GA4】 URLから文字列を取得して、イベントパラメータに入れる

GTM

※この記事には、修正版がございます。下記の方法でも動作は致しますが、設定についても、挙動についても、より効率的な方法がありますので、こちらの修正版の記事をご参照ください。

この記事では、URLの特定の位置にある文字列を取り出して、GA4のイベントのパラメータとして取得する方法を説明します。

今回の与件は、URLの例として、

https://ga4-quick.and-aaa.com/gtm-test/venezia/20220826/

というURL中の文字列(今回の場合は「/gtm-test/文字列/20220826/」)を取得して、イベントパメータに入れる方法を解説します。

URL から情報を取得

まずは情報を取得したいページ(商品ページなど)をユーザーが閲覧したときに、URL から 文字列 を取得する設定を行います。

ここでは URL から取得したい 文字列 を変数「textdata」として取得し、それをイベントのパラメータで使用できる形に格納します。

タグから新規タグを作成し、「カスタム HTML」を選択します。

今回の URL の場合は、HTML 部分に下記を記載します。

<script>
var textdata = '';
	
textdata = window.location.pathname.split('gtm-test/')[1].split('/')[0];
	
dataLayer.push({
		'get_from_url': window.textdata,
		'event': 'get_url_text'
	});
</script>

URL から情報を取得した時に行ったことを順を追って説明していきます。

まず「var textdata = ”;」で取得したい ID を定義します。この「textdata」は任意の文字列で大丈夫です。

次に URL からデータを取得します。

「window.location.pathname」で URL のパスを取得します。パスとはドメイン以下、今回の場合は「/gtm-test/文字列/20220826/」です。 

今回はパスの取得なので「.location.pathname」を使用しましたが、たとえば URL 全体を取得したい場合は「location.href」、パラメータ部分(?以降)を取得したい場合には「location.search」を使用します。

次に「/gtm-test/文字列/20220826/」の「文字列」部分のみを残すためにデータの加工をおこないます。「.split」で文字列を任意の場所で切り、その前([0])か後ろ([1])かを残すことができます。

今回の場合は「/gtm-test/」と「文字列」の後の「/」を除きたいので、.split(‘gtm-test/’)[1].split(‘/’)[0]と設定しました。

ここまでで URL からの取得とデータの加工は完了です。

次に GTM にデータを送信するため、下記のように記載します。

dataLayer.push({
		'get_from_url': window.textdata,
		'event': 'get_url_text'
	});

ここでは先ほど取得した変数 textdata をイベントのパラメータで使う「get_from_url」に入れ込みます。

イベントの設定(’event’: ‘get_url_text’)は、後で行います。

このイベントのトリガーは、下記のように設定しておきます。

ということで、このようなイベントになりました。

変数の設定

上記で取得した情報(textdata)をタグで使用できるようにするために、変数の設定をします。

「変数」から「ユーザー定義変数」の新規を選択し、変数タイプの「データレイヤーの変数」を選択します。

「データレイヤーの変数名」に先ほど作成した「get_from_url」と入力し、変数名も同じ「get_from_url」に設定したら変数の設定は完了です。

タグとトリガーの設定

次に、URLから取得した文字列をGA4に送信するイベントタグを設定します。

イベント名は、前出の’event’: ‘get_url_text’のところで決めてあったので、それを入力します。

イベントパラメータのパラメータ名で、「データレイヤーの変数名」として先程設定した、「get_from_url」を入力します。

これで、URLから取得した文字列をGA4に送信するイベントタグの設定ができました。

ここまで出来たら、タグを「公開」してください。

文字列取得の確認

DebugViewにて、文字列の取得が確認できたら成功です。

関連記事

特集記事

コメント

    • 通事
    • 2022.08.26 12:02pm

    質問なのですが、こちら正規表現の表やカスタムJavascriptではなくデータレイヤー変数を用いるのはなぜでしょうか? 変数で取得すればConfigタグでも設定できるようになるので便利かなと思っております。
    性能面で優位ということであればどれほど差が出るのかご教示いただきたいです。

  1. 貴重なご指摘をいただきまして、ありがとうございます。
    ご指摘いただきました通りで、上記の実装方法には、優位性はございません。
    修正版の記事を作成させていただきました。
    https://ga4-quick.and-aaa.com/archives/2892
    今後とも、よろしくお願い申し上げます。

  2. Im obliged for the post. Keep writing.

  3. Very neat blog.Thanks Again. Really Great.

  4. Really appreciate you sharing this post. Keep writing.

  5. Really enjoyed this post. Fantastic.

  6. I really like and appreciate your blog.Really thank you! Fantastic.

  7. Thanks so much for the article post.Thanks Again. Really Great.

  8. Really informative post. Fantastic.

  9. Major thanks for the blog article.Thanks Again. Really Cool.

  10. Really enjoyed this blog article.Really thank you! Fantastic.

  11. Thanks again for the article post.Much thanks again. Great.

  12. A big thank you for your blog.Really looking forward to read more. Keep writing.

  13. I really enjoy the blog article.Thanks Again. Really Cool.

  14. A big thank you for your post.Thanks Again. Cool.

  15. I think this is a real great post.Really thank you! Awesome.

  16. Thanks so much for the post.Much thanks again. Fantastic.

  17. Im thankful for the blog.Really thank you! Great.

  18. Hey there! I know this is kinda off topic but I’d figured I’d ask.
    Would you be interested in exchanging links or maybe guest
    authoring a blog post or vice-versa? My site addresses a lot of the
    same topics as yours and I feel we could greatly benefit from each other.
    If you’re interested feel free to shoot me an email.
    I look forward to hearing from you! Awesome blog by the way!

  19. I just like the valuable information you supply on your articles.
    I will bookmark your weblog and take a look at again right here regularly.

    I am slightly certain I’ll be informed a lot of new stuff right here!
    Good luck for the next!

  20. Hello to all, as I am in fact keen of reading this blog’s
    post to be updated on a regular basis. It carries pleasant data.

  21. Hey would you mind letting me know which hosting company you’re utilizing?
    I’ve loaded your blog in 3 completely different browsers
    and I must say this blog loads a lot quicker then most.
    Can you suggest a good internet hosting provider at a honest price?
    Thanks a lot, I appreciate it!

ランキング(週間)

  1. 1

    GA4 「ページ ロケーション」と「ページ パス と スクリーン クラス」何が違う?ドメインの有無以外にもパラメータの扱いに違いあり

  2. 2

    GA4の探索で「ページ別訪問数」を見ることは可能か?

  3. 3

    GA4 拡張計測機能の新顔「フォームの操作」 form_start, form_submit

  4. 4

    GA4のレポート画面の数値データは、イベント発生時から何時間後に確定するのか?

  5. 5

    GA4の指標「総ユーザー数」VS「アクティブ ユーザー数」

  6. 6

    【祝・GPT-4 が ChatGPT plus に登場記念!】アクセス解析コンサルタントが、GPT-4にCSVデータを渡して、データ解析結果のコメントを書いてもらう方法

  7. 7

    GA4 クロスドメイン設定では、リンク先に「_gl」パラメータが付く。勝手なパラメータを付けるとエラーになるページは要注意

  8. 8

    YouTubeの概要欄に貼られたリンクをクリックしてサイトを訪れたユーザーを計測するために

  9. 9

    GA4で異なるドメイン(サブドメイン)の同一ページパスをドメイン付きで表示する

  10. 10

    GA4 「ユーザー コンバージョン率」「セッションのコンバージョン率」の算出方法は?(2022年8月23日更新)

最近の記事

  1. 生成AIを調整してABテストを実装したら想像以上に早かった

  2. 無料で使えるABテストツールのバージョンアップを行いました

  3. 無料ABテストツール(and,B)のマニュアルを作成しました

カテゴリー

 
TOP