import React from 'react'; import FadeIn from '../../fade-in' import Bar from '../bar' import MoreLink from '../more-link' import numberFormatter from '../../util/number-formatter' import RocketIcon from '../modals/rocket-icon' import * as api from '../../api' import LazyLoader from '../../components/lazy-loader' export default class SearchTerms extends React.Component { constructor(props) { super(props) this.state = {loading: true} this.onVisible = this.onVisible.bind(this) this.fetchSearchTerms = this.fetchSearchTerms.bind(this) } onVisible() { this.fetchSearchTerms() if (this.props.query.period === 'realtime') { document.addEventListener('tick', this.fetchSearchTerms) } } componentDidUpdate(prevProps) { if (this.props.query !== prevProps.query) { this.setState({loading: true, terms: null}) this.fetchSearchTerms() } } componentWillUnmount() { document.removeEventListener('tick', this.fetchSearchTerms) } fetchSearchTerms() { api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/referrers/Google`, this.props.query) .then((res) => this.setState({ loading: false, searchTerms: res.search_terms || [], notConfigured: res.not_configured, isAdmin: res.is_admin })).catch((error) => { this.setState({ loading: false, searchTerms: [], notConfigured: true, error: true, isAdmin: error.payload.is_admin }) } ) } renderSearchTerm(term) { return (
{ term.name } {numberFormatter(term.visitors)}
) } renderList() { if (this.props.query.filters.goal) { return (
Sorry, we cannot show which keywords converted best for goal {this.props.query.filters.goal}
Google does not share this information
) } else if (this.state.notConfigured) { return (
This site is not connected to Search Console so we cannot show the search phrases. {this.state.isAdmin && this.state.error && <>

Please click below to connect your Search Console account.

}
{this.state.isAdmin && Connect with Google }
) } else if (this.state.searchTerms.length > 0) { const valLabel = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors' return (
Search term {valLabel}
{this.state.searchTerms.map(this.renderSearchTerm.bind(this))}
) } else { return (
Could not find any search terms for this period
Google Search Console data is sampled and delayed by 24-36h
Read more on our documentation
) } } renderContent() { if (this.state.searchTerms) { return (

Search Terms

{ this.renderList() }
) } } render() { return (
{ this.state.loading &&
} { this.renderContent() }
) } }