isEditorLoggedIn()

Indicates whether a Scrivito UI is present and an editor is logged in.

Copy
if (Scrivito.isEditorLoggedIn()) {
  return <span>Editor is logged in</span>;
}

Params

None

Returns

Boolean – true if the app is rendered inside the Scrivito UI, otherwise false.

Example

This method can be used to prevent that page access is tracked in editing mode. The code below exemplifies this for a GoogleAnalytics component – note the immediate return in componentDidMount.

Copy
import * as React from 'react';
import * as Scrivito from 'scrivito';
import Helmet from 'react-helmet';

class GoogleAnalytics extends React.Component {
  constructor(props) {
    super(props);

    this.state = { trackingId: '' };
  }

  componentDidMount() {
    if (Scrivito.isEditorLoggedin()) {
      return;
    }

    Scrivito.load(() => this.getTrackingId()).then(trackingId => {
      if (trackingId) {
        window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date;
        window.ga('create', trackingId, 'auto');
        window.ga('set', 'anonymizeIp', true);
        window.ga('require', 'urlChangeTracker');
        window.ga('send', 'pageview');

        this.setState({ trackingId });
      }
    });
  }

  render() {
    if (!this.state.trackingId) {
      return null;
    }

    return (
      <Helmet>
        <script async src='/google_analytics.js'></script>
        <script async src='https://www.google-analytics.com/analytics.js'></script>
      </Helmet>
    );
  }

  getTrackingId() {
    const rootPage = Scrivito.Obj.root();

    if (!rootPage) {
      return;
    }

    return rootPage.get('googleAnalyticsTrackingId');
  }
}

export default Scrivito.connect(GoogleAnalytics);