LoginSignup
9

More than 5 years have passed since last update.

PC/SPでURLを分ける際の注意事項

Posted at

PCサイトでスマホOSで来た場合にリダイレクトさせたい場合は

pc_script.js
if(/iphone|ipad|ipod|android/.test(navigator.userAgent.toLowerCase())){
    location.replace("/sp/");
}

という感じで書いていると、URLの?以降やハッシュタグがついていた場合消えてしまう。

pc_script.js
if(/iphone|ipad|ipod|android/.test(navigator.userAgent.toLowerCase())){
    location.replace("/sp/" + window.location.search + window.location.hash);
}

として、パラメータが消えないようにしましょう。

これをちゃんとやらないとGoogle Analyticsでちゃんとデータが取れなかったりするのでダメです。
また、同じ理由でGoogle Analyticsのscriptが読まれる前に上記リダイレクトのscriptを記載しましょう。

南無南無🙏

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9