Languages
[Edit]
EN

React - Google Ads / GPT with fluid size on web page

10 points
Created by:
chelsea
806

In this article we would like to show trick how in React use Google Ads / GPT in fluid size mode.

Fluid size for Google Ad / GPT achieved with transformation.
Fluid size for Google Ad / GPT achieved with transformation.

By default fluid size mode is not supported on web pages but there is some trick how to scale Ad with transform scale style. The main idea of the below solution is to display Google Ad in supported size defined in Google Ads Panel, keeping aspect ratio and scale Ad to the desired size - container size - size for below container component is set with style property. That approach is useful on mobile devices where we want to scale Ads to fill some area on out web page.

Notes:

  • if example doesn't work copy it to your web site and run it under your domain (it can do not work on localhost or in local file run in web browser),
  • go to this article to read about not scalled Google Ads / GPT in React,
  • go to this article to see component that scales content with transform scale style what was used in this aticle - ContentStretcher component.

Hint: in some cases it can be necessary to run slot.display(); when Ad containg DOM element is ready - you can try to use setTimeout with delay >=100ms as some trick.

Practical example: 

The article describes the situation in Feb 2021.

Note: be sure that Ad size (adWidth and adHeight) has always the same size to prevent unnecessary Ad recreation - or change it consciously according to new slot configurations.

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

ReactJS

React - Google Ads / GPT with fluid size on web page
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join