Skip to content

Instantly share code, notes, and snippets.

@furyutei
Last active January 26, 2022 07:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save furyutei/6a7eae95d1676be22b0844fc29cf374b to your computer and use it in GitHub Desktop.
Save furyutei/6a7eae95d1676be22b0844fc29cf374b to your computer and use it in GitHub Desktop.
[ユーザースクリプト] GoogleアナリティクスのカスタムURLパラメータを外してリダイレクト

[ユーザースクリプト] GoogleアナリティクスのカスタムURLパラメータを外してリダイレクト

GoogleアナリティクスのカスタムURLパラメータ(utm_*)がついたりつかなかったりするとブックマークが分散してしまって私的に嫌なので、これを除去したURLにリダイレクトするユーザースクリプトを作ってみました。

まぁスマホやタブレットでブックマークした場合は除去できないのであれですけどね……。

// ==UserScript==
// @name RemoveCustomParameters
// @name:ja カスタムパラメータ除去
// @namespace https://furyutei.com
// @author furyu
// @license MIT
// @version 0.0.2
// @include http://*
// @include https://*
// @description Redirect to URL without Google's custom parameter (utm_*)
// @description:ja Googleのカスタムパラメーター(utm_*)を取り除いたURLへリダイレクト
// @contributionURL https://memo.furyutei.com/about#send_donation
// @compatible chrome+tampermonkey
// @compatible firefox+violentmonkey
// ==/UserScript==
(()=>{
'use strict';
const RemoveParamterNames = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', ];
const
url_object = new URL(location.href),
search_params = url_object.searchParams,
hash_params = new URLSearchParams(url_object.hash.replace(/^#/,'')),
original_params_length = Array.from(search_params).length + Array.from(hash_params).length;
RemoveParamterNames.map(param_name => {
search_params.delete(param_name);
hash_params.delete(param_name);
});
if (original_params_length == (Array.from(search_params).length + Array.from(hash_params).length)) return;
const
hash_string = hash_params.toString();
url_object.hash = hash_string ? '#' + hash_string : '';
//location.replace(url_object.href);
location.href = url_object.href;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment