【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.

  46. Heya i am for the first time here. I came across this
    board and I find It truly useful & it helped me out a lot.
    I hope to give something back and aid others like
    you helped me.

  47. Say, you got a nice article.Thanks Again. Want more.

  48. A big thank you for your article post.Thanks Again. Want more.

  49. Wow, this post is pleasant, my sister is analyzing these things,
    therefore I am going to tell her.

  50. Hey! This is kind of off topic but I need some advice from an established
    blog. Is it hard to set up your own blog?
    I’m not very techincal but I can figure things out pretty quick.
    I’m thinking about creating my own but I’m not sure where to begin. Do you
    have any tips or suggestions? Cheers

  51. This design is incredible! You definitely know how to keep a reader amused.

    Between your wit and your videos, I was almost moved to start my own blog (well,
    almost…HaHa!) Great job. I really enjoyed what
    you had to say, and more than that, how you presented it.
    Too cool!

  52. Great blog. Great.

  53. Sicherer Online-Kauf von Medikamenten galena Oldenzaal Les risques associés
    à l’achat de médicaments en ligne

  54. Very nice article. I definitely love this website.
    Keep it up!

  55. You have a gift for explaining things. What Is Cryptocurrency

  56. My spouse and I stumbled over here coming from a
    different web address and thought I may as well check things out.
    I like what I see so now i am following you. Look forward to finding out about your web page repeatedly.

  57. hello!,I really like your writing very much!

    percentage we be in contact extra approximately your article
    on AOL? I need a specialist on this space to solve my problem.
    May be that is you! Taking a look ahead to see
    you.

  58. medicamentos sin prescripción médica en Chile zydus Cartagena Apotheke in Spanien für Medikamente

  59. I’m gone to tell my little brother, that he should also pay a quick visit this
    webpage on regular basis to obtain updated from newest news update.

  60. Preis von Medikamente auf Rezept Heumann Durbuy Sicherer
    Kauf von Medikamente

  61. Replica Sneakers eu952
    Replica Sneakers for women,
    fake jordans xc092

  62. баян аймагамбетова биография, аймагамбетов канат рахимович рация мчс
    слушать, кто работает на частоте
    400-470 мгц анна данченко биография год рождения, анна данченко телеведущая биография бұл өмірде аккорды укулеле, бір досым
    бар аккорды

  63. аққан жұлдыз басты кейіпкер, аққан жұлдыз 2 кітап
    мерседес кабан колеса, мерседес кабан 600 цена ұлдардың туған күніне
    не сыйлауға болады, ұлдарға жаңа жылға не сыйлауға болады онлайн подработка казахстан, удаленная работа в казахстане отзывы

  64. какие молитвы читают по усопшему до 9 дней сонник
    к чему снится муравьи 23 сентября 1995 знак зодиака, таблица знаков зодиака
    курсы нумерологии с сертификатом онлайн приснилась лягушка прыгает на меня большая

  65. achat de médicaments en ligne sans ordonnance Almus Zollikon dove trovare i farmaci in Svizzera

  66. работа подработка одинцовский район набережные челны дом
    мебели часы работы ночная подработка
    в минске с ежедневной платформа для заработка в интернете реальные отзывы

  67. Can you be more specific about the content of your article? After reading it, I still have some doubts. Hope you can help me.

  68. 항상 안전하게 게임하도록 노력하겠습니다! 방문하다 먹튀레이더

  69. Nice

  70. generic name for plaquenil hydroxychloroquine tablets

ランキング(週間)

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

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

  6. 6

    GA4とGoogle広告の連携・オーディエンス・ 広告用機械学習 | オンラインセミナー アーカイブ動画+テキスト起こし(2022年6月2日開催)

  7. 7

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

  8. 8

    GA4で設定したイベントの消し方(削除の仕方)

  9. 9

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

  10. 10

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

最近の記事

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

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

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

カテゴリー

 
TOP