Skip to Content
defold-typescript libraries
defold (official)

defold/extension-admob/admob

Functions and constants for interacting with Google AdMob APIs

  • GitHub: defold/extension-admob — pinned to 4.2.2 (released while Defold 1.13.1 was current)
    1. Pick a release from defold/extension-admob releases and add its Source code (zip) URL (or a packaged .zip asset, if the library ships one) to game.project under [project] dependencies, then Fetch Libraries in the Defold editor.
    2. Run bunx @defold-typescript/cli resolve to materialize its types.
    3. Import it under a namespace alias of your choice:
      import * as admob from "admob"

Functions

admob.initialize()

Initialize the Mobile Ads SDK. Warning: If you need to obtain consent from users in the European Economic Area (EEA), set any request-specific flags, or otherwise take action before loading ads, ensure you do so before initializing the Mobile Ads SDK. On Android, initialization starts automatically when admob.app_open_android is configured. Leave that setting empty and use admob.load_appopen() after initialization if consent or other configuration must be applied first. Original docs Android, iOS

admob.set_callback(callback: function)

Sets a callback function for receiving events from the SDK. Call admob.set_callback(nil) to remove callback

local function admob_callback(self, message_id, message)
    pprint(message_id, message)
    if message_id == admob.MSG_INITIALIZATION then
       if message.event == admob.EVENT_COMPLETE then
           print("EVENT_COMPLETE: Initialization complete")
       elseif message.event == admob.EVENT_JSON_ERROR then
           print("EVENT_JSON_ERROR: Internal NE json error "..message.error)
       end
   elseif message_id == admob.MSG_IDFA then
       if message.event == admob.EVENT_STATUS_AUTHORIZED then
           print("EVENT_STATUS_AUTHORIZED: ATTrackingManagerAuthorizationStatusAuthorized")
       elseif message.event == admob.EVENT_STATUS_DENIED then
           print("EVENT_STATUS_DENIED: ATTrackingManagerAuthorizationStatusDenied")
       elseif message.event == admob.EVENT_STATUS_NOT_DETERMINED then
           print("EVENT_STATUS_NOT_DETERMINED: ATTrackingManagerAuthorizationStatusNotDetermined")
       elseif message.event == admob.EVENT_STATUS_RESTRICTED then
           print("EVENT_STATUS_RESTRICTED: ATTrackingManagerAuthorizationStatusRestricted")
       elseif message.event == admob.EVENT_NOT_SUPPORTED then
           print("EVENT_NOT_SUPPORTED: IDFA request not supported on this platform or OS version")
       end
   elseif message_id == admob.MSG_INTERSTITIAL then
       if message.event == admob.EVENT_CLOSED then
           print("EVENT_CLOSED: Interstitial AD closed")
       elseif message.event == admob.EVENT_FAILED_TO_SHOW then
           print("EVENT_FAILED_TO_SHOW: Interstitial AD failed to show\nCode: "..message.code.."\nError: "..message.error)
       elseif message.event == admob.EVENT_OPENING then
           print("EVENT_OPENING: Interstitial AD is opening")
       elseif message.event == admob.EVENT_FAILED_TO_LOAD then
           print("EVENT_FAILED_TO_LOAD: Interstitial AD failed to load\nCode: "..message.code.."\nError: "..message.error)
       elseif message.event == admob.EVENT_LOADED then
           print("EVENT_LOADED: Interstitial AD loaded")
       elseif message.event == admob.EVENT_NOT_LOADED then
           print("EVENT_NOT_LOADED: can't call show_interstitial() before EVENT_LOADED\nError: "..message.error)
       elseif message.event == admob.EVENT_IMPRESSION_RECORDED then
           print("EVENT_IMPRESSION_RECORDED: Interstitial did record impression")
       elseif message.event == admob.EVENT_JSON_ERROR then
           print("EVENT_JSON_ERROR: Internal NE json error: "..message.error)
       end
   elseif message_id == admob.MSG_REWARDED then
       if message.event == admob.EVENT_CLOSED then
           print("EVENT_CLOSED: Rewarded AD closed")
       elseif message.event == admob.EVENT_FAILED_TO_SHOW then
           print("EVENT_FAILED_TO_SHOW: Rewarded AD failed to show\nCode: "..message.code.."\nError: "..message.error)
       elseif message.event == admob.EVENT_OPENING then
           print("EVENT_OPENING: Rewarded AD is opening")
       elseif message.event == admob.EVENT_FAILED_TO_LOAD then
           print("EVENT_FAILED_TO_LOAD: Rewarded AD failed to load\nCode: "..message.code.."\nError: "..message.error)
       elseif message.event == admob.EVENT_LOADED then
           print("EVENT_LOADED: Rewarded AD loaded")
       elseif message.event == admob.EVENT_NOT_LOADED then
           print("EVENT_NOT_LOADED: can't call show_rewarded() before EVENT_LOADED\nError: "..message.error)
       elseif message.event == admob.EVENT_EARNED_REWARD then
           print("EVENT_EARNED_REWARD: Reward: " .. tostring(message.amount) .. " " .. tostring(message.type))
       elseif message.event == admob.EVENT_IMPRESSION_RECORDED then
           print("EVENT_IMPRESSION_RECORDED: Rewarded did record impression")
       elseif message.event == admob.EVENT_JSON_ERROR then
           print("EVENT_JSON_ERROR: Internal NE json error: "..message.error)
       end
   elseif message_id == admob.MSG_BANNER then
       if message.event == admob.EVENT_LOADED then
           print("EVENT_LOADED: Banner AD loaded. Height: "..message.height.."px Width: "..message.width.."px")
       elseif message.event == admob.EVENT_OPENING then
           print("EVENT_OPENING: Banner AD is opening")
       elseif message.event == admob.EVENT_FAILED_TO_LOAD then
           print("EVENT_FAILED_TO_LOAD: Banner AD failed to load\nCode: "..message.code.."\nError: "..message.error)
       elseif message.event == admob.EVENT_CLICKED then
           print("EVENT_CLICKED: Banner AD loaded")
       elseif message.event == admob.EVENT_CLOSED then
           print("EVENT_CLOSED: Banner AD closed")
       elseif message.event == admob.EVENT_DESTROYED then
           print("EVENT_DESTROYED: Banner AD destroyed")
       elseif message.event == admob.EVENT_IMPRESSION_RECORDED then
           print("EVENT_IMPRESSION_RECORDED: Banner did record impression")
       elseif message.event == admob.EVENT_JSON_ERROR then
           print("EVENT_JSON_ERROR: Internal NE json error: "..message.error)
       end
   end
end

function init(self)
    if admob then
        admob.set_callback(admob_callback)
        admob.initialize()
    end
end

Parameters

  • callback: function — Callback function that is executed on any event in the SDK.
    • self: object — The calling script instance
    • message_id: number — One of message types: admob.MSG_INITIALIZATION initialization, admob.MSG_INTERSTITIAL message from Interstitial ad unit, admob.MSG_REWARDED message from Rewarded ad unit, admob.MSG_BANNER message from Banner ad unit
    • message: table — A table holding the data
      • event: number — One of event types: admob.EVENT_CLOSED, admob.EVENT_FAILED_TO_SHOW, admob.EVENT_OPENING, admob.EVENT_FAILED_TO_LOAD, admob.EVENT_LOADED, admob.EVENT_NOT_LOADED, admob.EVENT_EARNED_REWARD, admob.EVENT_COMPLETE, admob.EVENT_CLICKED, admob.EVENT_DESTROYED, admob.EVENT_IMPRESSION_RECORDED, admob.EVENT_JSON_ERROR
      • code?: number — The error code (if an error occurred or nil otherwise)
      • message?: string — The error message (if an error occurred or nil otherwise)

admob.set_privacy_settings(bool: boolean)

Sets user privacy preferences (must be called before admob.initialize()). Original docs Android, iOS

Parameters

  • bool: boolean

admob.request_idfa()

Display the App Tracking Transparency authorization request for accessing the IDFA. Original docs iOS

admob.show_ad_inspector()

Show Ad Inspector. This is an in-app overlay that enables authorized devices to perform realtime analysis of test ad requests directly within a mobile app. Ad Inspector only launces on test devices. Original docs Android, iOS

admob.load_appopen(ad_unit_id: string)

Starts loading an App Open Ad, can only be called after admob.MSG_INITIALIZATION event. This is the manual alternative to configuring admob.app_open_android or admob.app_open_ios, which automatically loads and shows App Open ads. Original docs Android, iOS

Parameters

  • ad_unit_id: string — Ad unit ID, for test ads use on Android "ca-app-pub-3940256099942544/9257395921", or on iOS "ca-app-pub-3940256099942544/5575463023" Original docs Android, iOS

admob.show_appopen()

Shows loaded App Open Ad, can only be called after admob.EVENT_LOADED Original docs Android, iOS

if admob and admob.is_appopen_loaded() then
    admob.show_appopen()
end

admob.is_appopen_loaded(): boolean

Checks if an App Open Ad is loaded and ready to show Original docs Android, iOS

Returns

  • is_ready: boolean

admob.load_interstitial(ad_unit_id: string)

Starts loading an Interstitial Ad, can only be called after admob.MSG_INITIALIZATION event Original docs Android, iOS

Parameters

  • ad_unit_id: string — Ad unit ID, for test ads use on Android "ca-app-pub-3940256099942544/1033173712", or on iOS "ca-app-pub-3940256099942544/4411468910" Original docs Android, iOS

admob.show_interstitial()

Shows loaded Interstitial Ad, can only be called after admob.EVENT_LOADED Original docs Android, iOS

if admob and admob.is_interstitial_loaded() then
    admob.show_interstitial()
end

admob.is_interstitial_loaded(): boolean

Checks if Interstitial Ad is loaded and ready to show Original docs Android, iOS

Returns

  • is_ready: boolean

admob.load_rewarded(ad_unit_id: string, options?: table)

Starts loading a Rewarded Ad, can only be called after admob.MSG_INITIALIZATION event Original docs Android, iOS

Parameters

  • ad_unit_id: string — Ad unit ID, for test ads use on Android "ca-app-pub-3940256099942544/5224354917", or on iOS "ca-app-pub-3940256099942544/1712485313" Original docs Android, iOS
  • options?: table — ServerSideVerificationOptions Android, iOS
    • user_id?: string — A unique identifier assigned to each user.
    • custom_data?: string — Custom Data attached to server-side reward callbacks.

admob.show_rewarded()

Shows loaded Rewarded Ad, can only be called after admob.EVENT_LOADED Original docs Android, iOS

if admob and admob.is_rewarded_loaded() then
    admob.show_rewarded()
end

admob.is_rewarded_loaded(): boolean

Checks if Rewarded Ad is loaded and ready to show Original docs Android, iOS

Returns

  • is_ready: boolean

admob.load_rewarded_interstitial(ad_unit_id: string)

Starts loading a Rewarded Interstitial Ad, can only be called after admob.MSG_INITIALIZATION event Original docs Android, iOS

Parameters

  • ad_unit_id: string — Ad unit ID, for test ads use on Android "ca-app-pub-3940256099942544/5354046379", or on iOS "ca-app-pub-3940256099942544/6978759866" Original docs Android, iOS

admob.show_rewarded_interstitial()

Shows loaded Rewarded Interstitial Ad, can only be called after admob.EVENT_LOADED Original docs Android, iOS

if admob and admob.is_rewarded_interstitial_loaded() then
    admob.show_rewarded_interstitial()
end

admob.is_rewarded_interstitial_loaded(): boolean

Checks if Rewarded Interstitial Ad is loaded and ready to show Original docs Android, iOS

Returns

  • is_ready: boolean

admob.load_banner(ad_unit_id: string, size?: number)

Starts loading a Banner Ad, can only be called after admob.MSG_INITIALIZATION event Original docs Android, iOS

Parameters

  • ad_unit_id: string — Ad unit ID, for adaptive banner test ads use on Android "ca-app-pub-3940256099942544/9214589741", or on iOS "ca-app-pub-3940256099942544/2435281174" Original docs Android, iOS
  • size?: number — Requested Banner Ad size, possible values: admob.SIZE_ADAPTIVE_BANNER (default), admob.SIZE_BANNER, admob.SIZE_FLUID, admob.SIZE_FULL_BANNER, admob.SIZE_LARGE_BANNER, admob.SIZE_LEADEARBOARD, admob.SIZE_MEDIUM_RECTANGLE, admob.SIZE_LARGE_ADAPTIVE_BANNER Original docs Android, iOS

admob.show_banner(position?: number)

Shows loaded Banner Ad, can only be called after admob.EVENT_LOADED Original docs Android, iOS

if admob and admob.is_banner_loaded() then
    admob.show_banner(admob.POS_TOP_CENTER)
end

Parameters

  • position?: number — Banner Ad position, possible values: admob.POS_NONE (default), admob.POS_TOP_LEFT, admob.POS_TOP_CENTER, admob.POS_TOP_RIGHT, admob.POS_BOTTOM_LEFT, admob.POS_BOTTOM_CENTER, admob.POS_BOTTOM_RIGHT, admob.POS_CENTER

admob.set_max_ad_content_rating(max_ad_rating: number)

Sets a maximum ad content rating. AdMob ads returned for your app will have a content rating at or below that level. Original docs Android, iOS

  admob.set_max_ad_content_rating(admob.MAX_AD_CONTENT_RATING_PG)

Parameters

  • max_ad_rating: number — Max Ad Rating, possible values: admob.MAX_AD_CONTENT_RATING_G, admob.MAX_AD_CONTENT_RATING_PG, admob.MAX_AD_CONTENT_RATING_T, admob.MAX_AD_CONTENT_RATING_MA

admob.hide_banner()

Temporarily hides Banner Ad, banner can be shown again using admob.show_banner() Original docs Android, iOS

admob.is_banner_loaded(): boolean

Checks if Banner Ad is loaded and ready to show Original docs Android, iOS

Returns

  • is_ready: boolean

admob.destroy_banner()

Hides and unloads Banner Ad (needs to call admob.load_banner() later to show Banner Ad) Original docs Android, iOS

Constants

admob.MSG_INITIALIZATION

admob.MSG_INTERSTITIAL

admob.MSG_REWARDED

admob.MSG_BANNER

admob.MSG_IDFA

admob.MSG_REWARDED_INTERSTITIAL

admob.MSG_APPOPEN

admob.EVENT_CLOSED

admob.EVENT_FAILED_TO_SHOW

admob.EVENT_OPENING

admob.EVENT_FAILED_TO_LOAD

admob.EVENT_LOADED

admob.EVENT_NOT_LOADED

admob.EVENT_EARNED_REWARD

admob.EVENT_COMPLETE

admob.EVENT_CLICKED

admob.EVENT_DESTROYED

admob.EVENT_JSON_ERROR

admob.EVENT_IMPRESSION_RECORDED

admob.EVENT_STATUS_AUTHORIZED

admob.EVENT_STATUS_DENIED

admob.EVENT_STATUS_NOT_DETERMINED

admob.EVENT_STATUS_RESTRICTED

admob.EVENT_NOT_SUPPORTED

admob.SIZE_ADAPTIVE_BANNER

admob.SIZE_LARGE_ADAPTIVE_BANNER

admob.SIZE_BANNER

admob.SIZE_FLUID

admob.SIZE_FULL_BANNER

admob.SIZE_LARGE_BANNER

admob.SIZE_LEADEARBOARD

admob.SIZE_MEDIUM_RECTANGLE

admob.POS_NONE

admob.POS_TOP_LEFT

admob.POS_TOP_CENTER

admob.POS_TOP_RIGHT

admob.POS_BOTTOM_LEFT

admob.POS_BOTTOM_CENTER

admob.POS_BOTTOM_RIGHT

admob.POS_CENTER

admob.MAX_AD_CONTENT_RATING_G

admob.MAX_AD_CONTENT_RATING_PG

admob.MAX_AD_CONTENT_RATING_T

admob.MAX_AD_CONTENT_RATING_MA

On this page

Functionsadmob.initialize()admob.set_callback(callback: function)admob.set_privacy_settings(bool: boolean)admob.request_idfa()admob.show_ad_inspector()admob.load_appopen(ad_unit_id: string)admob.show_appopen()admob.is_appopen_loaded(): booleanadmob.load_interstitial(ad_unit_id: string)admob.show_interstitial()admob.is_interstitial_loaded(): booleanadmob.load_rewarded(ad_unit_id: string, options?: table)admob.show_rewarded()admob.is_rewarded_loaded(): booleanadmob.load_rewarded_interstitial(ad_unit_id: string)admob.show_rewarded_interstitial()admob.is_rewarded_interstitial_loaded(): booleanadmob.load_banner(ad_unit_id: string, size?: number)admob.show_banner(position?: number)admob.set_max_ad_content_rating(max_ad_rating: number)admob.hide_banner()admob.is_banner_loaded(): booleanadmob.destroy_banner()Constantsadmob.MSG_INITIALIZATIONadmob.MSG_INTERSTITIALadmob.MSG_REWARDEDadmob.MSG_BANNERadmob.MSG_IDFAadmob.MSG_REWARDED_INTERSTITIALadmob.MSG_APPOPENadmob.EVENT_CLOSEDadmob.EVENT_FAILED_TO_SHOWadmob.EVENT_OPENINGadmob.EVENT_FAILED_TO_LOADadmob.EVENT_LOADEDadmob.EVENT_NOT_LOADEDadmob.EVENT_EARNED_REWARDadmob.EVENT_COMPLETEadmob.EVENT_CLICKEDadmob.EVENT_DESTROYEDadmob.EVENT_JSON_ERRORadmob.EVENT_IMPRESSION_RECORDEDadmob.EVENT_STATUS_AUTHORIZEDadmob.EVENT_STATUS_DENIEDadmob.EVENT_STATUS_NOT_DETERMINEDadmob.EVENT_STATUS_RESTRICTEDadmob.EVENT_NOT_SUPPORTEDadmob.SIZE_ADAPTIVE_BANNERadmob.SIZE_LARGE_ADAPTIVE_BANNERadmob.SIZE_BANNERadmob.SIZE_FLUIDadmob.SIZE_FULL_BANNERadmob.SIZE_LARGE_BANNERadmob.SIZE_LEADEARBOARDadmob.SIZE_MEDIUM_RECTANGLEadmob.POS_NONEadmob.POS_TOP_LEFTadmob.POS_TOP_CENTERadmob.POS_TOP_RIGHTadmob.POS_BOTTOM_LEFTadmob.POS_BOTTOM_CENTERadmob.POS_BOTTOM_RIGHTadmob.POS_CENTERadmob.MAX_AD_CONTENT_RATING_Gadmob.MAX_AD_CONTENT_RATING_PGadmob.MAX_AD_CONTENT_RATING_Tadmob.MAX_AD_CONTENT_RATING_MA