p3ol / react-access

⚛️ The easiest way to add Poool Access to your React app
MIT License
3 stars 2 forks source link
access paywall poool premium-content react

CI

Poool Access - React SDK

The easiest way to add Poool Access to your React app ✨

ℹ️ You're looking at the documentation for the v2+ of React Access. If you're looking for v1, please see the v1 docs.

Installation

yarn add @poool/react-access

Usage

import { useRef } from 'react';
import {
  AccessContext,
  RestrictedContent,
  Paywall,
  Pixel,
} from '@poool/react-access';

export default = () => {
  const contentRef = useRef();

  return (
    <>
      { /*
        Wrap everything with our AccessContext component. Note the withAudit
        prop which saves you from writing AuditContext inside of
        AccessContext
      */ }
      <AccessContext
        appId="insert_your_app_id"
        config={{ cookies_enabled: true }}
        withAudit={true}
      >
        { /* Wrap your content with our RestrictedContent component */ }
        <RestrictedContent ref={contentRef}>
          <div className="articleBody">
            <p>Your article content</p>
          </div>
        </RestrictedContent>

        { /*
          Place our <Paywall /> component where you want your paywall to be
          displayed
        */ }
        <Paywall contentRef={contentRef} />

        { /*
          Place our <Pixel /> component anywhere inside an <AuditContext />
          component (or <AccessContext withAudit={true} />) to track page-view
          events (used for native segmentation)
        */ }
        <Pixel type="page-view" data={{ type: 'premium' }} />
      </AccessContext>
    </>
  );
};

Usage with AuditContext

import { useRef } from 'react';
import {
  AccessContext,
  AuditContext,
  Paywall,
  RestrictedContent,
  Pixel,
} from '@poool/react-access';

export default () => {
  const contentRef = useRef();

  return (
    <AuditContext appId="insert_your_app_id">
      <AccessContext appId="insert_your_app_id">
        <RestrictedContent ref={contentRef}>
          <div className="articleBody">
            <p>Your article content</p>
          </div>
        </RestrictedContent>

        <Paywall contentRef={contentRef} />
        <Pixel type="conversion" />
      </AccessContext>
    </AuditContext>
  );
};

Documentation

<AccessContext />

Props

<AuditContext />

Props

<RestrictedContent />

Props

<Paywall />

Props

<Pixel />

Props

useAccess()

Can be used to retrieve some properties from the current access context, as well as the Access SDK itself.

Returns

Example

const { appId, lib: access } = useAccess();

useAudit()

Can be used to retrieve some properties from the current audit context, as well as the Audit SDK itself.

Returns

Example

const { appId, lib: audit } = useAudit();

Contributing

Please check the CONTRIBUTING.md doc for contribution guidelines.

Development

Install dependencies:

yarn install

Run examples at http://localhost:63000/ with webpack dev server:

yarn serve

And test your code:

yarn test

License

This software is licensed under MIT.

v2 -> v3 Migration

There shouldn't be any migration needed for this version.

v3 only drops support for Node 14 & yarn, so unless you want to contribute to this repo using Node 14 or Yarn, you don't have to do anything.

It also drops support for Internet Explorer, but as Access.js already dropped support for IE in 2019, this release only removes some useless IE polyfills.

v1 -> v2 Migration

Basic example in v1:

import {
  PaywallContext,
  RestrictedContent,
  Paywall,
  usePoool,
} from '@poool/react-access';

export default () => {
  const { poool } = usePoool();

  useEffect(() => {
    poool('config', 'context', 'sports');
    poool('send', 'page-view', 'premium');
  }, []);

  return (
    <PaywallContext appId="test" config={{ cookies_enabled: true }}>
      <RestrictedContent><div>test</div></RestrictedContent>
      <Paywall />
    </PaywallContext>
  );
};

To be transformed in v2:

import { useRef } from 'react';
import {
  AccessContext,
  RestrictedContent,
  Paywall,
  Pixel,
} from '@poool/react-access';

export default () => {
  const contentRef = useRef();

  return (
    <AccessContext
      appId="test"
      config={{ cookies_enabled: true }}
      withAudit={true}
    >
      <RestrictedContent ref={contentRef}><div>test</div></RestrictedContent>

      <Paywall config={{ context: 'sports' }} contentRef={contentRef} />
      <Pixel type="page-view" data={{ type: 'premium' }} />
    </AccessContext>
  );
};