How to store referrer url in NUXT js?

1.4k views Asked by At

I want to store the referrer url in local storage. My project is based on nuxt js. This is what I have so far. I tried localStorage.setItem but it is not seeming to work. How can I solve this?

 if (this.$route.query.ref) {
     localStorage.setItem("referrer", '$route.query.referrer');
      this.$axios
        .$post("/affiliates/createClick", {
          referral_code: this.$route.query.ref
        })
        .then(response => {
          this.click_id = response.data.id;
          console.log(this.click_id);
          // this.referral_code = this.$route.query.ref;
          const options = {
            path: "/",
            maxAge: 60 * 60 * 24 * 45
          };
          const cookieList = [
            {
              name: "affiliate_id",
              value: this.$route.query.ref,
              opts: options
            },
            { name: "click_id", value: this.click_id, opts: options }
          ];
          this.$cookies.setAll(cookieList);
        });
    } 
    else if (this.$route.query.referrer) {
      // sessionStorage.setItem("referrer", '$route.query.referrer');
      console.log(this.$route.query.referrer);
      // this.referral_code = this.$route.query.ref;
      const options = {
        path: "/",
        maxAge: 60 * 60 * 24 * 45
      };
      const cookieList = [
        {
          name: "referrer",
          value: this.$route.query.referrer,
          opts: options
        }
      ];
      this.$cookies.setAll(cookieList);
    }
  },
1

There are 1 answers

0
Istiake On

This issue here is likely that at some point you set an string in local storage that is not JSON-parseable. You Can try this

localStorage.setItem('referrer', JSON.stringify('$route.query.referrer'))