🐶
blog.terrier.dev

posted: 2018/04/05

gatsbyでwindowを使うとError: ReferenceError: window is not defined出るのを何とかする


gatsbyでreact-gaのようなwindowを利用するようなものを扱おうとすると、Webpackが下記のように怒ってくる
WebpackError: window is not defined
gatsbyはwebpackまわりが隠蔽されているので困っていたが、下記で解決策を見つけた
https://github.com/gatsbyjs/gatsby/issues/309#issuecomment-302043875
if (typeof window !== "undefined") {
  ReactGA.initialize("UA-XXXXXX", { debug: true });
  // @ts-ignore
  ReactGA.pageview(window.location.pathname + window.location.search);
}
ということでこんな感じになる

Edit on Github
@terrierscript