Skip to content

Event handler called multiple times if path are same. #31

@mark-liner

Description

@mark-liner

First of all, thank you for your great module. I couldn't have finished my project without it.

When creating ads on infinite scroll screen, I had to use same ad units in Google Ad Manager.

  if (onSlotRenderEnded) {
    googletag.cmd.push(() => {
      googletag.pubads().addEventListener('slotRenderEnded', (event) => {
        // check if the current slot is the one the callback was added to
        // (as addEventListener is global)
        if (event.slot.getAdUnitPath() === path) {
          onSlotRenderEnded(event);
        }
      });
    });
  }

Above code only check the path of the ad unit, so I added && statement to validate the slotElementId.

  // Execute callback when the slot is visible in DOM (thrown before 'impressionViewable' )
  if (onSlotRenderEnded) {
    googletag.cmd.push(() => {
      googletag.pubads().addEventListener('slotRenderEnded', (event) => {
        // check if the current slot is the one the callback was added to
        // (as addEventListener is global)

        if (event.slot.getAdUnitPath() === path && event.slot.getSlotElementId() === id) {
          onSlotRenderEnded(event);
        }
      });
    });
  }

Also, when the Ad component unmounts, the event handler do not get removed. So it would be a good idea to remove the event handler in componentWillUnmount.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions