From a550c1bb245ae475ded6af637556f941f34a54da Mon Sep 17 00:00:00 2001 From: Bosko Milekic Date: Mon, 2 Nov 2020 13:04:18 -0500 Subject: [PATCH] Always load GAM ads even when targeting fails. (#22) - Update vanilla GAM360 targeting demo page - Update README --- README.md | 40 +++++++++++++++++-------- demos/vanilla/targeting/gam360.html | 34 ++++++++++++++------- demos/vanilla/targeting/gam360.html.tpl | 34 ++++++++++++++------- 3 files changed, 74 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 1f8affe..dd7668f 100644 --- a/README.md +++ b/README.md @@ -207,7 +207,9 @@ The Optable Web SDK can fetch targeting keyvalue data from a sandbox and send it ### Targeting key values -Loading the Optable SDK via a `script tag` on a web page which also uses the [Google Publisher Tag](https://developers.google.com/doubleclick-gpt/guides/get-started), we can further extend the `targeting` example above to show an integration with a [Google Ad Manager 360](https://admanager.google.com/home/) ad server account: +Loading the Optable SDK via a `script tag` on a web page which also uses the [Google Publisher Tag](https://developers.google.com/doubleclick-gpt/guides/get-started), we can further extend the `targeting` example above to show an integration with a [Google Ad Manager 360](https://admanager.google.com/home/) ad server account. + +It's suggested to load the GAM banner view with an ad even when the call to your sandbox `targeting()` method raises an exception, as shown in the example below: ```html @@ -242,20 +244,34 @@ Loading the Optable SDK via a `script tag` on a web page which also uses the [Go
diff --git a/demos/vanilla/targeting/gam360.html.tpl b/demos/vanilla/targeting/gam360.html.tpl index 34a5c84..b3e7df2 100644 --- a/demos/vanilla/targeting/gam360.html.tpl +++ b/demos/vanilla/targeting/gam360.html.tpl @@ -24,19 +24,31 @@ optable.instance.installGPTEventListeners(); }); + // Helper to load GAM ads with optional targeting data: + var loadGAM = function (tdata = {}) { + // Sets up page-level targeting in GAM360 GPT: + window.googletag = window.googletag || { cmd: [] }; + googletag.cmd.push(function () { + for (const [key, values] of Object.entries(tdata)) { + googletag.pubads().setTargeting(key, values); + console.log("[OptableSDK] googletag.pubads().setTargeting(" + key + ", [" + values + "])"); + } + googletag.pubads().refresh(); + console.log("[OptableSDK] googletag.pubads().refresh()"); + }); + }; + + // Try to fetch targeting data from sandbox and pass it to GAM: optable.cmd.push(function () { - optable.instance.targeting().then(function (result) { - // Sets up page-level targeting in GAM360 GPT: - window.googletag = window.googletag || { cmd: [] }; - googletag.cmd.push(function () { - for (const [key, values] of Object.entries(result)) { - googletag.pubads().setTargeting(key, values); - console.log("[OptableSDK] googletag.pubads().setTargeting(" + key + ", [" + values + "])"); - } - googletag.pubads().refresh(); - console.log("[OptableSDK] googletag.pubads().refresh()"); + optable.instance + .targeting() + .then(function (result) { + loadGAM(result); + }) + .catch((err) => { + console.log("[OptableSDK] targeting() exception: " + err.message); + loadGAM(); }); - }); });