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

GTM

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

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

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

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

変数の設定

「カスタム JavaScript」で変数を設定します。

コードは下記のようになります。

function () {
  return window.location.pathname.split('gtm-test/')[1].split('/')[0];
}

「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-test’という文字列が入っていないと利用できない形です。こちらで汎用性が大きくなるようなスクリプト(カスタムJavascript変数)も紹介させていただきます。

function () {

  /*
   * ページパスは基本的に'/'で区切られるので
   * window.location.pathnameでページパスを取得し
   * さらにそれを".split('/')"することで
   * 本例の場合以下のような配列の形になる
   * [ '', 'gtm-test', 'venezia', '20220826', '' ]
   */
  var texts = window.location.pathname.split('/');

  /*
   * 今回欲しい値は上のtextsの3番目(index相当で2)のため
   * "[2]"で配列の3番目を取得し、それを返す
   * この"2"を変更すること他のURLパターンの場合でも対応可能
   */ 
  return texts[2];
}

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

タグとトリガーの設定

イベントパラメータで、先程設定した変数「get_from_url2」の値を取得できるようにします。

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

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

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

文字列取得の確認

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

関連記事

特集記事

コメント

  1. Im grateful for the article post.Really thank you!

  2. Enjoyed every bit of your article post. Cool.

  3. A round of applause for your article.Much thanks again. Cool.

  4. Thanks for sharing, this is a fantastic article.Much thanks again. Really Great.

  5. A round of applause for your article post.Really looking forward to read more. Want more.

  6. Im thankful for the blog post. Really Cool.

  7. I value the article post.Really looking forward to read more.

  8. I appreciate you sharing this blog article.Really looking forward to read more. Great.

  9. Thanks so much for the article post. Want more.

  10. Thank you ever so for you article post. Fantastic.

  11. I really like and appreciate your article post.Really thank you! Great.

  12. I loved your article post.Really thank you! Really Cool.

  13. I really liked your post.Much thanks again. Much obliged.

  14. I really enjoy the blog article.Really thank you! Cool.

  15. writing an essay onlinecollege essay writingwriting essays online

  16. An interesting discussion is worth comment. I think that you ought to write more about this subject matter, it might not be a taboo matter but usually folks don’t talk about such topics. To the next! All the best!!

  17. Just bookmarked this post as I have found it quite helpful.

  18. Enjoyed every bit of your article post.Really looking forward to read more. Want more.

  19. Thanks for sharing, this is a fantastic blog post.Really thank you! Will read on…

  20. Thanks so much and I am looking ahead to touch you.judi online indonesia

  21. Remarkable! Its really amazing piece of writing, I have got much clear idea regarding from this piece of writing.

  22. Maxims and aphorisms let us remember that wisdom is the true salt of literature and the books that are most nourishing are richly stored with it and that is the main object to seek in reading books.

  23. I live in London ivermectin manfaat Whomever the Nets pick as captain for this squad — and they have an incumbent in Williams and the longtime Celtics captain in Paul Pierce — it’s really just a figurehead position unless it goes to Garnett.

  24. Hey just wanted to give you a quick heads up and let you know a few of the images aren’tloading correctly. I’m not sure why but I think its a linkingissue. I’ve tried it in two different internet browsers and bothshow the same outcome.

  25. Thanks-a-mundo for the blog post.Thanks Again. Will read on…

  26. Im thankful for the blog post.Thanks Again. Want more.

  27. Good blog you have here.. Itís difficult to find quality writing like yours these days. I truly appreciate people like you! Take care!!

  28. Thanks a lot for the article.Thanks Again.

  29. 1st and main attraction is made for certain the material employed.

  30. Hi! Do you know if they make any plugins to protect against hackers? I’m kinda paranoid about losing everything I’ve worked hard on. Any recommendations?

  31. 693726 596299Woh Everybody loves you , bookmarked ! My partner and i take problem in your last point. 658795

  32. stromectol tab – ivermectin pills ivermectin for covid 19

  33. Thanks-a-mundo for the post.Really thank you!

  34. to a single Sony PSN Card… about this since the very

  35. Great, thanks for sharing this blog.Really looking forward to read more. Fantastic.

  36. I loved your blog.Really thank you! Cool.

  37. Hey! I just wanted to ask if you ever have any trouble with hackers?My last blog (wordpress) was hacked and I ended up losing afew months of hard work due to no back up.Do you have any methods to stop hackers?

  38. RoyalCBD says:You should take part in a contest for one of the finest blogs on the internet. I’m going to recommend this blog!Reply 08/13/2020 at 3:56 am

  39. effects of plaquenil canada pharmacy online does plaquenil make you gain weight how long do the side effects of plaquenil last

  40. Muchos Gracias for your article.Much thanks again. Really Cool.

  41. treatments for ed: ed medication – top rated ed pills

  42. Always love reading your blogs whenever I get some time because the way you describe information in your blogs, it helps me a lot in improving my writing skills as well. AC Repair Weston

  43. An interesting discussion is definitely worth comment. I believe that you need to publish more on this subject, it may not be a taboo subject but typically folks don’t speak about such subjects. To the next! All the best.

  44. The the very next time I read a blog, Hopefully which it doesnt disappoint me around brussels. What i’m saying is, It was my choice to read, but I just thought youd have some thing interesting to convey. All I hear can be a couple of whining about something you could fix in the event you werent too busy in search of attention.

  45. It’s very trouble-free to find out any matter on net as compared to books, as I found this piece of writing at this web site.

ランキング(週間)

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

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

  7. 7

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

  8. 8

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

  9. 9

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

  10. 10

    Ads Data Hub(ADH) とはどんなものですか?GA4との関連は?

最近の記事

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

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

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

カテゴリー

 
TOP