From e00bd8620ce696a364662fae2ac954e09ef9ca87 Mon Sep 17 00:00:00 2001 From: Bosko Milekic Date: Mon, 9 Nov 2020 21:12:30 -0500 Subject: [PATCH] targetingFromCache() and targetingClearCache() APIs - targeting API now also caches returned keyvalues to client storage - targetingFromCache will fetch previously cached keyvalues from client storage. Contrary to the targeting API, it is synchronous - targetingClearCache will clear any previously cached keyvalues - Update Kotlin and Java demo apps to show usage of targeting*Cache - Update README with targeting*Cache docs --- .../ui/GAMBanner/GAMBannerFragment.java | 67 ++++++++++++++----- .../main/res/layout/fragment_gambanner.xml | 31 ++++++++- .../ui/GAMBanner/GAMBannerFragment.kt | 53 ++++++++++++--- .../main/res/layout/fragment_gambanner.xml | 31 ++++++++- README.md | 42 ++++++++++++ android_sdk/build.gradle | 2 +- .../java/co/optable/android_sdk/Config.kt | 13 +++- .../java/co/optable/android_sdk/OptableSDK.kt | 9 +++ .../co/optable/android_sdk/core/Client.kt | 12 ++++ .../optable/android_sdk/core/LocalStorage.kt | 26 +++++++ 10 files changed, 254 insertions(+), 32 deletions(-) diff --git a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java index 6842c14..46520d1 100644 --- a/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java +++ b/DemoApp/DemoAppJava/app/src/main/java/co/optable/demoappjava/ui/GAMBanner/GAMBannerFragment.java @@ -14,6 +14,7 @@ import com.google.android.gms.ads.doubleclick.PublisherAdView; import java.util.HashMap; +import java.util.List; import co.optable.android_sdk.OptableSDK; import co.optable.demoappjava.MainActivity; @@ -30,6 +31,7 @@ public View onCreateView(@NonNull LayoutInflater inflater, mPublisherAdView = root.findViewById(R.id.publisherAdView); targetingDataView = root.findViewById(R.id.targetingDataView); + // loadAdButton loads targeting data and then the GAM banner: Button btn = root.findViewById(R.id.loadAdButton); btn.setOnClickListener(view -> { targetingDataView.setText(""); @@ -51,27 +53,60 @@ public View onCreateView(@NonNull LayoutInflater inflater, targetingDataView.setText(msg.toString()); mPublisherAdView.loadAd(adRequest.build()); + witness(); }); + }); - HashMap eventProperties = new HashMap(); - eventProperties.put("exampleKey", "exampleValue"); - - MainActivity.OPTABLE - .witness("GAMBannerFragment.loadAdButtonClicked", eventProperties) - .observe(getViewLifecycleOwner(), result -> { - final StringBuilder msg = new StringBuilder(); - msg.append(targetingDataView.getText().toString()); - - if (result.getStatus() == OptableSDK.Status.SUCCESS) { - msg.append("\n\nSuccess calling witness API to log loadAdButtonClicked event.\n\n"); - } else { - msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n"); - } + // loadAdButton2 loads targeting data from cache, and then the GAM banner: + btn = root.findViewById(R.id.loadAdButton2); + btn.setOnClickListener(view -> { + targetingDataView.setText(""); + PublisherAdRequest.Builder adRequest = new PublisherAdRequest.Builder(); + final StringBuilder msg = new StringBuilder(); + HashMap> data = MainActivity.OPTABLE.targetingFromCache(); + + if (data != null) { + msg.append("Loading GAM ad with cached targeting data:\n\n"); + data.forEach((key, values) -> { + adRequest.addCustomTargeting(key, values); + msg.append(key.toString() + " = " + values.toString()); + }); + } else { + msg.append("Targeting data cache empty."); + } + + targetingDataView.setText(msg.toString()); + mPublisherAdView.loadAd(adRequest.build()); + witness(); + }); - targetingDataView.setText(msg.toString()); - }); + // loadAdButton3 clears targeting data cache: + btn = root.findViewById(R.id.loadAdButton3); + btn.setOnClickListener(view -> { + targetingDataView.setText("Clearing targeting data cache.\n\n"); + MainActivity.OPTABLE.targetingClearCache(); }); return root; } + + private void witness() { + HashMap eventProperties = new HashMap(); + eventProperties.put("exampleKey", "exampleValue"); + + MainActivity.OPTABLE + .witness("GAMBannerFragment.loadAdButtonClicked", eventProperties) + .observe(getViewLifecycleOwner(), result -> { + final StringBuilder msg = new StringBuilder(); + msg.append(targetingDataView.getText().toString()); + + if (result.getStatus() == OptableSDK.Status.SUCCESS) { + msg.append("\n\nSuccess calling witness API to log loadAdButtonClicked event.\n\n"); + } else { + msg.append("\n\nOptableSDK Error: " + result.getMessage() + "\n\n"); + } + + targetingDataView.setText(msg.toString()); + }); + } } \ No newline at end of file diff --git a/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml b/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml index cd1a0f3..93b9687 100644 --- a/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml +++ b/DemoApp/DemoAppJava/app/src/main/res/layout/fragment_gambanner.xml @@ -22,10 +22,37 @@