skip to Main Content

I’m getting issue on adding more than 2 adsterra ad banner on Next.js 14 app. When I’m trying to render two Adsterra ad banner’s on a same page,the first one is’nt rendering.
I also tried with next/script, getting same issue

First banner code:

"use client";

import classNames from "classnames";
import { HTMLAttributes, useEffect, useRef } from "react";

const BannerAd468x60 = (props: HTMLAttributes<HTMLDivElement>) => {
  const { className } = props;
  const container = useRef<HTMLDivElement>(null);

  const isWindow = typeof window !== "undefined";

  const atOptions = {
    key: "abc",
    format: "iframe",
    height: 60,
    width: 468,
    params: {},
  };

  useEffect(() => {
    if (isWindow && container.current && !container.current.firstChild) {
      const conf = document.createElement("script");
      const script = document.createElement("script");
      script.type = "text/javascript";
      script.src = `//www.topcreativeformat.com/${atOptions.key}/invoke.js`;
      script.async = true;
      script.defer = true;
      conf.async = true;
      conf.defer = true;
      conf.innerHTML = `atOptions = ${JSON.stringify(atOptions)}`;

      container.current.append(conf);
      container.current.append(script);
    }
  }, [container, isWindow]);

  return (
    <div
      ref={container}
      className={classNames(
        "flex items-center justify-center",
        className && className
      )}
    />
  );
};

export default BannerAd468x60;

Second Banner code:

"use client";

import classNames from "classnames";
import { HTMLAttributes, useEffect, useRef } from "react";

const BannerAd728x90 = (props: HTMLAttributes<HTMLDivElement>) => {
  const { className } = props;
  const container = useRef<HTMLDivElement>(null);

  const isWindow = typeof window !== "undefined";

  const atOptions = {
    key: "abc",
    format: "iframe",
    height: 90,
    width: 728,
    params: {},
  };

  useEffect(() => {
    if (isWindow && container.current && !container.current.firstChild) {
      const conf = document.createElement("script");
      const script = document.createElement("script");
      script.type = "text/javascript";
      script.src = `//www.topcreativeformat.com/${atOptions.key}/invoke.js`;
      script.async = true;
      script.defer = true;
      conf.async = true;
      conf.defer = true;
      conf.innerHTML = `atOptions = ${JSON.stringify(atOptions)}`;

      container.current.append(conf);
      container.current.append(script);
    }
  }, [container, isWindow]);

  return (
    <div
      ref={container}
      className={classNames(
        "flex items-center justify-center",
        className && className
      )}
    />
  );
};

export default BannerAd728x90;

Unable to add more than 2 adsterra ad banner on Next.js 14 app

2

Answers


  1. Chosen as BEST ANSWER

    I found the solution, It's working for me.

    import classNames from "classnames";
    import { HTMLAttributes } from "react";
    
    const BannerAd468x60 = (props: HTMLAttributes<HTMLDivElement>) => {
      const { className } = props;
    
      return (
        <div
          className={classNames(
            "flex items-center justify-center",
            className && className
          )}
        >
          <iframe
            src={`//www.topcreativeformat.com/watchnew?key=<KEY>`}
            width="468px"
            height="60px"
            scrolling="no"
          />
        </div>
      );
    };
    
    export default BannerAd468x60;


  2. Dear try using this this may work cause it works for me 🙂

    "use client";
    
    import classNames from "classnames";
    import { HTMLAttributes, useEffect, useRef } from "react";
    import Script from 'next/script';
    
    const BannerAd468x60 = (props: HTMLAttributes<HTMLDivElement>) => {
      const { className } = props;
      const container = useRef<HTMLDivElement>(null);
    
      const atOptions = {
        key: "unique_key_1",
        format: "iframe",
        height: 60,
        width: 468,
        params: {},
      };
    
      return (
        <div
          ref={container}
          className={classNames(
            "flex items-center justify-center",
            className && className
          )}
        >
          <Script
            id="ad-script-468x60"
            strategy="lazyOnload"
            dangerouslySetInnerHTML={{
              __html: `
                var atOptions = ${JSON.stringify(atOptions)};
                (function() {
                  var script = document.createElement('script');
                  script.type = 'text/javascript';
                  script.src = '//www.topcreativeformat.com/' + atOptions.key + '/invoke.js';
                  script.async = true;
                  script.defer = true;
                  container.current.append(script);
                })();
              `,
            }}
          />
        </div>
      );
    };
    
    export default BannerAd468x60;
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search