2020-08-17 15:13:30 +03:00
|
|
|
import React from 'react';
|
|
|
|
import FadeIn from '../../fade-in'
|
|
|
|
import Bar from '../bar'
|
|
|
|
import MoreLink from '../more-link'
|
2021-12-03 14:59:32 +03:00
|
|
|
import numberFormatter from '../../util/number-formatter'
|
2020-08-17 15:13:30 +03:00
|
|
|
import RocketIcon from '../modals/rocket-icon'
|
|
|
|
import * as api from '../../api'
|
2023-01-02 18:42:57 +03:00
|
|
|
import LazyLoader from '../../components/lazy-loader'
|
2020-08-17 15:13:30 +03:00
|
|
|
|
|
|
|
export default class SearchTerms extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
|
|
|
this.state = {loading: true}
|
2023-01-02 18:42:57 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
|
|
|
this.fetchSearchTerms = this.fetchSearchTerms.bind(this)
|
2020-08-17 15:13:30 +03:00
|
|
|
}
|
|
|
|
|
2023-01-02 18:42:57 +03:00
|
|
|
onVisible() {
|
2020-08-17 15:13:30 +03:00
|
|
|
this.fetchSearchTerms()
|
2023-01-02 18:42:57 +03:00
|
|
|
if (this.props.query.period === 'realtime') {
|
|
|
|
document.addEventListener('tick', this.fetchSearchTerms)
|
|
|
|
}
|
2020-08-17 15:13:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
|
|
|
this.setState({loading: true, terms: null})
|
|
|
|
this.fetchSearchTerms()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 18:42:57 +03:00
|
|
|
componentWillUnmount() {
|
|
|
|
document.removeEventListener('tick', this.fetchSearchTerms)
|
|
|
|
}
|
|
|
|
|
2020-08-17 15:13:30 +03:00
|
|
|
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,
|
2021-06-16 15:00:07 +03:00
|
|
|
isAdmin: res.is_admin
|
2023-01-02 18:42:57 +03:00
|
|
|
})).catch((error) =>
|
2022-10-24 10:34:02 +03:00
|
|
|
{
|
|
|
|
this.setState({ loading: false, searchTerms: [], notConfigured: true, error: true, isAdmin: error.payload.is_admin })
|
|
|
|
}
|
|
|
|
)
|
2020-08-17 15:13:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
renderSearchTerm(term) {
|
|
|
|
return (
|
|
|
|
<div className="flex items-center justify-between my-1 text-sm" key={term.name}>
|
2021-07-08 09:42:30 +03:00
|
|
|
<Bar
|
2021-11-04 15:20:39 +03:00
|
|
|
count={term.visitors}
|
2021-07-08 09:42:30 +03:00
|
|
|
all={this.state.searchTerms}
|
|
|
|
bg="bg-blue-50 dark:bg-gray-500 dark:bg-opacity-15"
|
|
|
|
maxWidthDeduction="4rem"
|
|
|
|
>
|
2021-07-29 09:54:52 +03:00
|
|
|
<span className="flex px-2 py-1.5 dark:text-gray-300 z-9 relative break-all">
|
|
|
|
<span className="md:truncate block">
|
2020-08-17 15:13:30 +03:00
|
|
|
{ term.name }
|
|
|
|
</span>
|
|
|
|
</span>
|
2021-07-08 09:42:30 +03:00
|
|
|
</Bar>
|
2021-11-04 15:20:39 +03:00
|
|
|
<span className="font-medium dark:text-gray-200">{numberFormatter(term.visitors)}</span>
|
2020-08-17 15:13:30 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
renderList() {
|
|
|
|
if (this.props.query.filters.goal) {
|
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="text-center text-gray-700 dark:text-gray-300 text-sm mt-20">
|
2020-08-17 15:13:30 +03:00
|
|
|
<RocketIcon />
|
|
|
|
<div>Sorry, we cannot show which keywords converted best for goal <b>{this.props.query.filters.goal}</b></div>
|
|
|
|
<div>Google does not share this information</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
|
|
|
|
} else if (this.state.notConfigured) {
|
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="text-center text-gray-700 dark:text-gray-300 text-sm mt-20">
|
2020-08-17 15:13:30 +03:00
|
|
|
<RocketIcon />
|
2022-10-24 10:34:02 +03:00
|
|
|
<div>
|
|
|
|
This site is not connected to Search Console so we cannot show the search phrases.
|
|
|
|
{this.state.isAdmin && this.state.error && <><br/><br/><p>Please click below to connect your Search Console account.</p></>}
|
|
|
|
</div>
|
2021-06-16 15:00:07 +03:00
|
|
|
{this.state.isAdmin && <a href={`/${encodeURIComponent(this.props.site.domain)}/settings/search-console`} className="button mt-4">Connect with Google</a> }
|
2020-08-17 15:13:30 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
} else if (this.state.searchTerms.length > 0) {
|
2020-08-18 14:14:56 +03:00
|
|
|
const valLabel = this.props.query.period === 'realtime' ? 'Current visitors' : 'Visitors'
|
2020-08-17 15:13:30 +03:00
|
|
|
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="flex items-center mt-3 mb-2 justify-between text-gray-500 dark:text-gray-400 text-xs font-bold tracking-wide">
|
2020-08-17 15:13:30 +03:00
|
|
|
<span>Search term</span>
|
|
|
|
<span>{valLabel}</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{this.state.searchTerms.map(this.renderSearchTerm.bind(this))}
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<div className="text-center text-gray-700 dark:text-gray-300 text-sm mt-20">
|
2020-08-17 15:13:30 +03:00
|
|
|
<RocketIcon />
|
|
|
|
<div>Could not find any search terms for this period</div>
|
|
|
|
<div>Google Search Console data is sampled and delayed by 24-36h</div>
|
2022-08-15 04:20:40 +03:00
|
|
|
<div>Read more on <a href="https://plausible.io/docs/google-search-console-integration#i-dont-see-google-search-query-data-in-my-dashboard" target="_blank" rel="noreferrer" className="hover:underline text-indigo-700 dark:text-indigo-500">our documentation</a></div>
|
2020-08-17 15:13:30 +03:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
|
|
|
if (this.state.searchTerms) {
|
|
|
|
return (
|
|
|
|
<React.Fragment>
|
2020-12-16 12:57:28 +03:00
|
|
|
<h3 className="font-bold dark:text-gray-100">Search Terms</h3>
|
2020-08-17 15:13:30 +03:00
|
|
|
{ this.renderList() }
|
|
|
|
<MoreLink site={this.props.site} list={this.state.searchTerms} endpoint="referrers/Google" />
|
|
|
|
</React.Fragment>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-06-15 10:34:43 +03:00
|
|
|
<div
|
|
|
|
className="stats-item flex flex-col relative bg-white dark:bg-gray-825 shadow-xl rounded p-4 mt-6 w-full"
|
|
|
|
>
|
2020-08-17 15:13:30 +03:00
|
|
|
{ this.state.loading && <div className="loading mt-44 mx-auto"><div></div></div> }
|
2021-06-15 10:34:43 +03:00
|
|
|
<FadeIn show={!this.state.loading} className="flex-grow">
|
2023-01-02 18:42:57 +03:00
|
|
|
<LazyLoader onVisible={this.onVisible}>
|
|
|
|
{ this.renderContent() }
|
|
|
|
</LazyLoader>
|
2020-08-17 15:13:30 +03:00
|
|
|
</FadeIn>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|