skip to Main Content

When testing locally, I need to turn on the testing mode, so I need to add 'data-adbreak-test': 'on' to the script.
However, this attribute needs to be deleted in a production environment.

dev:

{
    async: true,
    src: 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
    'data-ad-client': process.env.NUXT_PUBLIC_ADSENSE_CLIENT_ID,
    'data-ad-frequency-hint': '30s',
    'data-adbreak-test': 'on'  // open testing mode
},

prod

{
    async: true,
    src: 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
    'data-ad-client': process.env.NUXT_PUBLIC_ADSENSE_CLIENT_ID,
    'data-ad-frequency-hint': '30s',
    // 'data-adbreak-test': 'on'  // close testing mode
},

It needs to be commented out every time before going online. The operation is too cumbersome, and it is easy to forget to comment. Cause errors in the production environment. Is there a value similar to 'off' to disable testing mode? In this case, I can control whether testing mode needs to be turned on through Nuxt environment variables.

I checked the documentation and didn’t find any other values ​​besides 'on'google adsense doc

Or is there any other way that can be automatically controlled without manual annotation?

I have tried 'data-adbreak-test': 'off' and 'data-adbreak-test': '' with no effect

2

Answers


  1. Chosen as BEST ANSWER

    I solved this problem, if there is a better way, please feel free to comment.

    {
        async: true,
        src: 'https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js',
        'data-ad-client': process.env.NUXT_PUBLIC_ADSENSE_CLIENT_ID,
        'data-ad-frequency-hint': '30s',
        ...(process.env.NUXT_ENV === 'development' ? { 'data-adbreak-test': 'on' } : {}),
    },
    

  2. // Check if we are in the production environment
    const isProduction = process.env.NODE_ENV === 'production';
    
    // Get a reference to the script element
    const script = document.getElementById('your-script-id'); // Replace 'your-script-id' with the actual ID of your script element
    
    // Conditionally add or remove the attribute
    if (!isProduction) {
      script.setAttribute('data-adbreak-test', 'on');
    } else {
      script.removeAttribute('data-adbreak-test');
    }
    
    You can try this
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search