Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
3.2 kB
2
Indexable
Never

  componentDidMount() {
    if (this.props.wcmMode === 'author') {
      this.setState({
        hero: this.props.items[0],
        defaultBanner: this.props.items[0],
      });
    } else {
      this.setState({
        hero: this.props.items[0],
        defaultBanner: this.props.items[0],
      });
      this.setTargetedBanner();
    }
  }

  checkTargetedSegments(userData) {
    let segmentAudienceFlag = false;
    let segmentAudienceFlagOne = false;
    let hero = this.state.defaultBanner;
    this.props.items.map(heroBanner => {
      let matchingCookies = 0;
      if (heroBanner.audienceSegments) {
        for (let audience of heroBanner.audienceSegments) {
          const cookieValue = audience.segmentKey && getCookie(audience.segmentKey);
          const isEqualTo = audience.segmentValue.includes(cookieValue);
          if (isEqualTo) {
            matchingCookies += 1;
          } else if (audience.segmentValue === 'default' && audience.segmentKey === 'default') {
            this.setState({
              defaultBanner: heroBanner,
            });
            break;
          }
        }
      }
      if (heroBanner.audienceSegments && matchingCookies === heroBanner.audienceSegments.length) {
        this.setState({
          hero: heroBanner,
        });
        hero = heroBanner;
        segmentAudienceFlag = true;
      } else {
        if (heroBanner.segmentedUsers && !segmentAudienceFlag && userData) {
          let matchedUserCount = 0;
          for (let data of userData) {
            for (let user of heroBanner.segmentedUsers) {
              user === data ? matchedUserCount++ : matchedUserCount;
            }
          }
          // if (matchedUserCount == heroBanner.segmentedUsers.length) {
          if (matchedUserCount > 0 && !segmentAudienceFlagOne) {
            segmentAudienceFlagOne = true;
            this.setState({
              hero: heroBanner,
            });
            hero = heroBanner;
          }
        }
      }
    });
    if (!this.state.hero && this.state.defaultBanner) {
      this.setState({
        hero: this.state.defaultBanner,
      });
      hero = this.state.defaultBanner;
    }
    targetedBannerImpression(hero.link);
  }

  setTargetedBanner() {
    if (sessionStorage.getItem('segmentedUser')) {
      const segmentedUserList = JSON.parse(sessionStorage.getItem('segmentedUser'));
      this.checkTargetedSegments(segmentedUserList.segments);
    } else {
      const eKey = getCookie('email');
      const mKey = window['visitor'] && window['visitor'].getMarketingCloudVisitorID();
      const API_URL = `${Constants.PERSONALIZED_API_DOMAIN}/api/personalized/v1/segments?ekey=${eKey}&mkey=${mKey}`;
      fetch(API_URL, {
        method: 'GET',
        headers: {
          Accept: 'application/json',
        },
      })
        .then(response => response.json())
        .then(data => {
          if (data && data.segments && data.segments.length) {
            this.checkTargetedSegments(data.segments);
            sessionStorage.setItem('segmentedUser', JSON.stringify(data));
          } else {
            this.checkTargetedSegments();
          }
        })
        .catch(() => {
          this.checkTargetedSegments();
        });
    }
  }