Allow asynchronous loading of GPT

Created on 29 October 2014, over 10 years ago
Updated 20 July 2025, 8 days ago

The current GPT JavaScript implementation is not following Google's recommendations, it seems like a mixture of both synchronous and asynchronous code. This page provides examples of both modes. Please note that in asynchronous mode API calls are wrapped in googletag.cmd.push() all the time.

Current implementation has the .enableServices() call outside of that wrapper which may result in firing ads before all the configuration has occurred. When in synchronous mode, there should be no wrapper at all.

Also, the Google examples use an inline snippet for asynchronous loading of gpt.js to maximise comaptibility which could work fine with every browser where the async attribute is not supported.

I suggest to fix the inline code first as per the recommendations and then offer the option to switch between synchronous and asynchronous loading.

Detailed examples of code output

Please study the attached example code files and see how the syntax changes compared to the current implementation (also attached).

Current implementation vs New, synchronous output:

  • Final service API call is no longer wrapped in the googletag command queue (i.e googletag.cmd.push())
  • Slot display code is no longer wrapped in the googletag command queue (i.e googletag.cmd.push())

Current implementation vs New, asynchronous output:

  • Loading the library is changed to load asynchronously
  • Ad slot definition code is wrapped around the googletag command queue
  • Final service API call is wrapped around the googletag command queue, including googletag.enableServices();
  • Slot display code is wrapped around the googletag command queue
Feature request
Status

Closed: outdated

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom melon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024