Git Product home page Git Product logo

react-dfp-slot's Introduction

react-dfp-slot

npm version

A React component library to execute Google DFP logic.

Getting started

  1. Install package

    yarn add react-dfp-slot

    or

    npm install react-dfp-slot
  2. Set the DFP provider

    // App.js
    import { DFPProvider } from 'react-dfp-slot';
    
    render() {
      return (
        <DFPProvider>
          <section className={appClassName}>
            ...
          </section>
        </DFPProvider>
      );
    }
  3. Add AdSlot component with profile

    import { AdSlot } from 'react-dfp-slot';
    
    class SomeComponent extends Component {
      ...
      render() {
        return (
          <section className="some-component-main">
            ...
            <AdSlot
              profile={{
                path: '/1000000/adunit',
                size: ['fluid'],
                waitingFor: 'detail',
                hideOnInitial: true,
                className: 'native-style',
              }}
            />
          </section>
        );
      }
    }

DFPProvider

Child context

  getChildContext() {
    return {
      getIsSlotAdReady: this.getIsSlotAdReady,
      setIsSlotAdReady: this.setIsSlotAdReady,
      getIsComponentMounted: this.getIsComponentMounted,
      setIsComponentMounted: this.setIsComponentMounted,
      refreshAds: this.refreshAds,
      clearAdSlots: this.clearAdSlots,
    };
  }

getIsSlotAdReady(slotName)

Get target slot is ready to display or not

  • slotName (string): target slot name

setIsSlotAdReady(slotName, isReady)

Set target slot is ready to display or not

  • slotName (string): target slot name
  • isReady (bool)

getIsComponentMounted(complonentName)

Get target component is mounted or not

  • componentName (string): target component name

setIsComponentMounted(complonentName, doRefreshAd)

Set target component is mounted or not

  • componentName (string): target component name
  • doRefreshAd (bool)

refreshAds()

Refresh all unrefreshed slots

clearAds()

Clear all unrefreshed slots


Props

propTypes: {
  /**
   * Enables or disables collapsing of slot divs so that they don't
   * take up any space on the page when there is no ad content to display.
   */
  collapseEmptyDivs: ProTypes.bool,

  /**
   * Event handler for slotRenderEnded event.
   */
  onSlotRenderEnded: PropTypes.func,
},

onSlotRenderEnded(provider, event)

  • provider: child context from DFPProvider
  • event: GPT event
// example
handleSlotRenderEnded = (provider, event) => {
    const adElement = document.getElementById(`${event.slot.getSlotElementId()}-container`);

    switch (event.slot.getAdUnitPath()) {
      case AdProfiles.idleBannerCenter.path: {
        provider.setIsSlotAdReady(AdProfiles.idleBannerCenter.name, !event.isEmpty);
        break;
      }
      default:
        break;
    }

    if (event.isEmpty && adElement) {
      adElement.style.display = 'block';
    }
};

AdSlot

Props

  // [300, 250], [[300, 250], 'fluid'], ['fluid]
  const SIZE_TYPE = PropTypes.arrayOf(
    PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.number), PropTypes.string, PropTypes.number]),
  );


  /* if responsive break point is [0, 0] and [1300, 0] ([width, height])
  [
    [
      [0,0],
      [[1, 1], [650, 60], 'fluid'],
    ],
    [
      [1300, 0],
      [[1, 1], [800, 60], 'fluid']
    ]
  ]
  */
  const MULTI_SIZE_TYPE = PropTypes.arrayOf(PropTypes.arrayOf(AD_SIZE_TYPE));


  const ProfilePropType = PropTypes.shape({
    path: PropTypes.string,           // DFP code
    name: PropTypes.string,           // slot name
    size: SIZE_TYPE,                  // slot size
    multiSize: MULTI_SIZE_TYPE,       // responsive size mapping
    multiSizeHandler: PropTypes.func, // responsive handler
    waitingFor: PropTypes.string,     // refresh ads after component didmount
    hideOnInitial: PropTypes.bool,    // set display: none; on initial
  });

  propTypes: {
    // Profile data for slot
    profile: ProfilePropType.isRequired,

    // class name
    className: PropTypes.string,

    // Init slot after specific millisecond
    asyncInit: PropTypes.number,

    // use lazyLoading mode
    lazyLoading: PropTypes.bool,

    /**
     * `topOffset` can either be a number, in which case its a distance from the
     * top of the container in pixels, or a string value. Valid string values are
     * of the form "20px", which is parsed as pixels, or "20%", which is parsed
     * as a percentage of the height of the containing element.
     * For instance, if you pass "-20%", and the containing element is 100px tall,
     * then the waypoint will be triggered when it has been scrolled 20px beyond
     * the top of the containing element.
     */
    lazyLoadingTopOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),

    /**
     * `bottomOffset` is like `topOffset`, but for the bottom of the container.
     */
    lazyLoadingBottomOffset: PropTypes.oneOfType([PropTypes.number, PropTypes.string]),

    // DFP setTargeting
    targetValue: PropTypes.arrayOf(PropTypes.string),

  },

react-dfp-slot's People

Contributors

hwrdy avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.