-
-
Save grantkemp/cf21340ef8eed4501f2de5a108f914c2 to your computer and use it in GitHub Desktop.
Glootie snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Firebase | |
... | |
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { | |
... | |
FirebaseApp.configure() | |
if !UserDefaults.standard.bool(forKey: "launchedBefore") | |
{ | |
UserDefaults.standard.set(true, forKey: "launchedBefore") | |
GoogleAdsACService.app.adToAction(processAction: processActionExample) | |
} | |
return true | |
} | |
func processActionExample (action : String) { | |
guard let product = MockItemProvider.all()[action.lowercased()] else { | |
return | |
} | |
let storyboard = UIStoryboard(name: "Main", bundle: nil); | |
let viewController: ProductDetailsViewController = storyboard.instantiateViewController(withIdentifier: "productDetails") as! ProductDetailsViewController; | |
viewController.product = product | |
// Then push that view controller onto the navigation stack | |
let rootViewController = self.window!.rootViewController as! UINavigationController; | |
rootViewController.pushViewController(viewController, animated: true); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.demo.firebaseproject.googleads; | |
import android.app.Activity; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import com.google.android.gms.ads.identifier.AdvertisingIdClient; | |
import com.google.android.gms.common.GooglePlayServicesNotAvailableException; | |
import com.google.android.gms.common.GooglePlayServicesRepairableException; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.functions.FirebaseFunctions; | |
import com.google.firebase.functions.FirebaseFunctionsException; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.concurrent.TimeUnit; | |
public class GoogleAdsACService { | |
private static final String AD_TO_ACTION = "adToAction"; | |
private static final String TAG = "GoogleAdsACService"; | |
private static GoogleAdsACService instance = null; | |
private FirebaseFunctions mFunctions; | |
private GoogleAdsACService() { | |
mFunctions = FirebaseFunctions.getInstance(); | |
} | |
public static GoogleAdsACService getInstance() { | |
if (instance == null) { | |
instance = new GoogleAdsACService(); | |
} | |
return instance; | |
} | |
public void adToAction(Activity context, ActionCallback callback) { | |
AsyncTask.execute(() -> { | |
try { | |
AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(context); | |
String adId = adInfo != null ? adInfo.getId() : null; | |
if (adId != null) { | |
this.getAdInfoCloudFunction(adId, adInfo.isLimitAdTrackingEnabled()) | |
.addOnCompleteListener(task -> { | |
if (!task.isSuccessful()) { | |
Exception e = task.getException(); | |
if (e instanceof FirebaseFunctionsException) { | |
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e; | |
FirebaseFunctionsException.Code code = ffe.getCode(); | |
Log.w(TAG, "adToAction:onFailure: " + | |
"details: "+ ffe.getDetails() + "; code: "+ code); | |
} else { | |
Log.w(TAG, "adToAction:onFailure", e); | |
} | |
return; | |
} | |
Map<String, Object> result = task.getResult(); | |
Log.w(TAG, "adToAction: " + result); | |
if(result.get("action") != null) { | |
//result map holds: campaignId, action, timestamp | |
//for more complex logic or event tagging concider to return all the result map to callback | |
callback.commit(result.get("action")); | |
} | |
}); | |
} | |
} catch (IOException | GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException exception) { | |
Log.w(TAG, "Get User Attribution Failed: ", exception); | |
} | |
}); | |
} | |
private Task<Map<String, Object>> getAdInfoCloudFunction(String advertisingId, boolean lat) { | |
// Create the arguments to the callable function, which are two integers | |
Map<String, Object> data = new HashMap<>(); | |
data.put("advertisingId", advertisingId); | |
data.put("lat", lat ? 1 : 0); | |
// Call the function and extract the operation from the result | |
return mFunctions | |
.getHttpsCallable(AD_TO_ACTION) | |
.withTimeout(2, TimeUnit.SECONDS) | |
.call(data) | |
.continueWith(task -> (Map<String, Object>) task.getResult().getData()); | |
} | |
public interface ActionCallback { | |
void commit(Object action); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// GoogleAdsACService.swift | |
// iosadsdemo | |
// | |
// Created by Lior Kupfer on 26/04/2020. | |
// Copyright © 2020 wentaoli. All rights reserved. | |
// | |
import AdSupport; | |
import FirebaseFunctions | |
class GoogleAdsACService { | |
let VOID_IDFA = "00000000-0000-0000-0000-000000000000"; | |
public static var app: GoogleAdsACService = { | |
return GoogleAdsACService() | |
}() | |
public func adToAction (processAction: @escaping (_ action: String) -> Void) { | |
let idfaOpt = identifierForAdvertising() | |
guard let idfa = idfaOpt, idfa != VOID_IDFA else { | |
print("IDFA not available") | |
return | |
} | |
getAdInfoCloudFunction(idfa: idfa, processAction: processAction) | |
} | |
private func getAdInfoCloudFunction(idfa: String, processAction: @escaping (_ action: String) -> Void) { | |
let functions = Functions.functions() | |
let httpsAdToAction = functions.httpsCallable("adToAction") | |
httpsAdToAction.timeoutInterval = 2 | |
httpsAdToAction.call(["advertisingId": idfa, "lat" : 0]) { (result, error) in | |
if let error = error as NSError? { | |
if error.domain == FunctionsErrorDomain { | |
let message = error.localizedDescription | |
NSLog("Firebsase Function Error: %@", message) | |
} | |
} | |
if let action = (result?.data as? [String: Any])?["action"] as? String { | |
//result holds: campaignId, action, timestamp | |
//for more complex logic or event tagging concider to return all the result object to callback | |
processAction(action) | |
} | |
} | |
} | |
private func identifierForAdvertising() -> String? { | |
// Check whether advertising tracking is enabled | |
guard ASIdentifierManager.shared().isAdvertisingTrackingEnabled else { | |
return nil | |
} | |
// Get and return IDFA | |
return ASIdentifierManager.shared().advertisingIdentifier.uuidString | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private static final String TAG = "MainActivity"; | |
private static final String FIRST_OPEN = "FIRST_OPEN"; | |
private static final String FIRST_OPEN_KEY = "FIRST_OPEN_ACTION"; | |
private static SharedPreferences prefs = null; | |
... | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
... | |
prefs = getSharedPreferences(FIRST_OPEN_KEY, MODE_PRIVATE); | |
if (prefs.getBoolean(FIRST_OPEN, true)) { | |
prefs.edit().putBoolean(FIRST_OPEN, false).apply(); | |
GoogleAdsACService.getInstance() | |
.adToAction(this, this::googleAdToAction); | |
} | |
} | |
private void googleAdToAction(Object action) { | |
try { | |
if (action instanceof String) { | |
String productName = (String) action; | |
Product product = new MockInventory() | |
.getProductByName(productName); | |
ProductActivity.navigateToProductPage(MainActivity.this, product.id); | |
Log.d(TAG, "Init app on item: " + productName); | |
} | |
} catch (NullPointerException e) { | |
Log.d(TAG, "Invalid item name or item name not found"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"adGroupIds": { | |
"111111111": "Sweatshirt", | |
"123456789": "Notebook" | |
}, | |
"campaignIds": { | |
"111111111": "Sweatshirt", | |
"123456789": "Notebook" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
target 'iosadsdemo' do | |
# Comment the next line if you don't want to use dynamic frameworks | |
use_frameworks! | |
# Pods for iosadsdemo | |
pod 'Firebase/Functions' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment