Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mark-liner opened this issue Nov 2, 2022 · 0 comments
Open

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

mark-liner opened this issue Nov 2, 2022 · 0 comments

Comments

@mark-liner
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant