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)- Pick a release from defold/extension-admob releases and add its Source code (zip) URL (or a packaged
.zipasset, if the library ships one) togame.projectunder[project]dependencies, then Fetch Libraries in the Defold editor. - Run
bunx @defold-typescript/cli resolveto materialize its types. - Import it under a namespace alias of your choice:
import * as admob from "admob"
- Pick a release from defold/extension-admob releases and add its Source code (zip) URL (or a packaged
Functions
admob.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()
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
endParameters
callback:function— Callback function that is executed on any event in the SDK.self:object— The calling script instancemessage_id:number— One of message types:admob.MSG_INITIALIZATIONinitialization,admob.MSG_INTERSTITIALmessage from Interstitial ad unit,admob.MSG_REWARDEDmessage from Rewarded ad unit,admob.MSG_BANNERmessage from Banner ad unitmessage:table— A table holding the dataevent: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_ERRORcode?:number— The error code (if an error occurred ornilotherwise)message?:string— The error message (if an error occurred ornilotherwise)
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
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()
endadmob.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
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()
endadmob.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, iOSoptions?:table— ServerSideVerificationOptions Android, iOSuser_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()
endadmob.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
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()
endadmob.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, iOSsize?: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_BANNEROriginal 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)
endParameters
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
admob.destroy_banner()
Hides and unloads Banner Ad (needs to call admob.load_banner() later to show Banner Ad) Original docs Android, iOS