PageSourceSearch

https://nocode-sol.co.jp/wp-content/mu-plugins/nocode-utm-fill.js?ver=1.2.0

js nocode-sol.co.jp collected 2026-10-04 05:18:49 UTC 2,883 bytes, 73 lines download raw bytes

1/**
2 * UTMパラメータ・流入ページをWPFormsの隠しフィールドに自動入力する。
3 *
4 * v1.2.0(2026-10-02): フォームIDごとのマップを読むようにした。
5 *   v1.1.0 はグローバルに1組のIDしか持てなかったが、フィールドIDはフォームごとに違う
6 *   (実測で9フォームが0〜16までばらばら)。1本のマップで全フォームを埋めると、
7 *   別フォームの同じ番号のフィールド(氏名や電話)へ書き込んでしまう。
8 *
9 * - landing_page は「セッション内で最初に着地したURL」をsessionStorageに保存して使う
10 * - utm_source / utm_medium / utm_campaign は現在のURLのクエリから取る
11 *
12 * 前提:
13 *   window.NOCODE_UTM_FIELD_IDS_BY_FORM = { "398": { landing_page: 14 }, "5880": { landing_page: 12 } }
14 *   が <script> で先に注入されていること(mu-plugin の wp_add_inline_script が出力)。
15 *   マップに無いフォームには一切触らない。
16 */
17(function () {
18  var MAP = window.NOCODE_UTM_FIELD_IDS_BY_FORM;
19  if (!MAP || typeof MAP !== 'object') return;
20
21  var params = new URLSearchParams(location.search);
22
23  // セッションで最初に着地したURLを保持する。2ページ目以降は上書きしない
24  try {
25    if (!sessionStorage.getItem('nocode_landing')) {
26      sessionStorage.setItem('nocode_landing', location.href);
27    }
28  } catch (e) {
29    // プライベートウィンドウ等でsessionStorageが使えないことがある。
30    // その場合は現在のURLで代用する(空で送るより手がかりが残る)
31  }
32
33  function landing() {
34    try {
35      return sessionStorage.getItem('nocode_landing') || location.href;
36    } catch (e) {
37      return location.href;
38    }
39  }
40
41  function valueFor(key) {
42    if (key === 'landing_page') return landing();
43    return params.get(key) || '';
44  }
45
46  function fillAll() {
47    Object.keys(MAP).forEach(function (formId) {
48      // WPFormsのDOM: <form id="wpforms-form-398"> 。フォームの中だけを触る
49      var form = document.getElementById('wpforms-form-' + formId);
50      if (!form) return;
51      var ids = MAP[formId] || {};
52      Object.keys(ids).forEach(function (key) {
53        var fid = ids[key];
54        if (fid === null || fid === undefined) return;
55        var val = valueFor(key);
56        if (!val) return;
57        var inputs = form.querySelectorAll('input[name="wpforms[fields][' + fid + ']"]');
58        for (var i = 0; i < inputs.length; i++) {
59          if (!inputs[i].value) inputs[i].value = val;
60        }
61      });
62    });
63  }
64
65  if (document.readyState === 'loading') {
66    document.addEventListener('DOMContentLoaded', fillAll);
67  } else {
68    fillAll();
69  }
70
71  // WPFormsがAjaxでフォームを後から差し込むケースのフォールバック
72  setTimeout(fillAll, 800);
73})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.