Search in sources :

Example 1 with AttributionIdentifiers

use of com.facebook.internal.AttributionIdentifiers in project facebook-api-android-maven by avianey.

the class AppEventsLogger method getSessionEventsState.

// Creates a new SessionEventsState if not already in the map.
private static SessionEventsState getSessionEventsState(Context context, AccessTokenAppIdPair accessTokenAppId) {
    // Do this work outside of the lock to prevent deadlocks in implementation of
    //  AdvertisingIdClient.getAdvertisingIdInfo, because that implementation blocks waiting on the main thread,
    //  which may also grab this staticLock.
    SessionEventsState state = stateMap.get(accessTokenAppId);
    AttributionIdentifiers attributionIdentifiers = null;
    if (state == null) {
        // Retrieve attributionId, but we will only send it if attribution is supported for the app.
        attributionIdentifiers = AttributionIdentifiers.getAttributionIdentifiers(context);
    }
    synchronized (staticLock) {
        // Check state again while we're locked.
        state = stateMap.get(accessTokenAppId);
        if (state == null) {
            state = new SessionEventsState(attributionIdentifiers, context.getPackageName(), hashedDeviceAndAppId);
            stateMap.put(accessTokenAppId, state);
        }
        return state;
    }
}
Also used : AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers)

Example 2 with AttributionIdentifiers

use of com.facebook.internal.AttributionIdentifiers in project facebook-api-android-maven by avianey.

the class Settings method publishInstallAndWaitForResponse.

static Response publishInstallAndWaitForResponse(final Context context, final String applicationId, final boolean isAutoPublish) {
    try {
        if (context == null || applicationId == null) {
            throw new IllegalArgumentException("Both context and applicationId must be non-null");
        }
        AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(context);
        SharedPreferences preferences = context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
        String pingKey = applicationId + "ping";
        String jsonKey = applicationId + "json";
        long lastPing = preferences.getLong(pingKey, 0);
        String lastResponseJSON = preferences.getString(jsonKey, null);
        // prevent auto publish from occurring if we have an explicit call.
        if (!isAutoPublish) {
            setShouldAutoPublishInstall(false);
        }
        GraphObject publishParams = GraphObject.Factory.create();
        publishParams.setProperty(ANALYTICS_EVENT, MOBILE_INSTALL_EVENT);
        Utility.setAppEventAttributionParameters(publishParams, identifiers, Utility.getHashedDeviceAndAppID(context, applicationId), getLimitEventAndDataUsage(context));
        publishParams.setProperty(AUTO_PUBLISH, isAutoPublish);
        publishParams.setProperty("application_package_name", context.getPackageName());
        String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
        Request publishRequest = Request.newPostRequest(null, publishUrl, publishParams, null);
        if (lastPing != 0) {
            GraphObject graphObject = null;
            try {
                if (lastResponseJSON != null) {
                    graphObject = GraphObject.Factory.create(new JSONObject(lastResponseJSON));
                }
            } catch (JSONException je) {
            // return the default graph object if there is any problem reading the data.
            }
            if (graphObject == null) {
                return Response.createResponsesFromString("true", null, new RequestBatch(publishRequest), true).get(0);
            } else {
                return new Response(null, null, null, graphObject, true);
            }
        } else if (identifiers == null || (identifiers.getAndroidAdvertiserId() == null && identifiers.getAttributionId() == null)) {
            throw new FacebookException("No attribution id available to send to server.");
        } else {
            if (!Utility.queryAppSettings(applicationId, false).supportsAttribution()) {
                throw new FacebookException("Install attribution has been disabled on the server.");
            }
            Response publishResponse = publishRequest.executeAndWait();
            // denote success since no error threw from the post.
            SharedPreferences.Editor editor = preferences.edit();
            lastPing = System.currentTimeMillis();
            editor.putLong(pingKey, lastPing);
            // if we got an object response back, cache the string of the JSON.
            if (publishResponse.getGraphObject() != null && publishResponse.getGraphObject().getInnerJSONObject() != null) {
                editor.putString(jsonKey, publishResponse.getGraphObject().getInnerJSONObject().toString());
            }
            editor.commit();
            return publishResponse;
        }
    } catch (Exception e) {
        // if there was an error, fall through to the failure case.
        Utility.logd("Facebook-publish", e);
        return new Response(null, null, new FacebookRequestError(null, e));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers)

Example 3 with AttributionIdentifiers

use of com.facebook.internal.AttributionIdentifiers in project facebook-android-sdk by facebook.

the class AppEventsLogger method updateUserProperties.

public static void updateUserProperties(final Bundle parameters, final String applicationID, final GraphRequest.Callback callback) {
    final String userID = getUserID();
    if (userID == null || userID.isEmpty()) {
        Logger.log(LoggingBehavior.APP_EVENTS, TAG, "AppEventsLogger userID cannot be null or empty");
        return;
    }
    getAnalyticsExecutor().execute(new Runnable() {

        @Override
        public void run() {
            Bundle userPropertiesParams = new Bundle();
            userPropertiesParams.putString("user_unique_id", userID);
            userPropertiesParams.putBundle("custom_data", parameters);
            // This call must be run on the background thread
            AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(FacebookSdk.getApplicationContext());
            if (identifiers != null && identifiers.getAndroidAdvertiserId() != null) {
                userPropertiesParams.putString("advertiser_id", identifiers.getAndroidAdvertiserId());
            }
            Bundle data = new Bundle();
            try {
                JSONObject userData = BundleJSONConverter.convertToJSON(userPropertiesParams);
                JSONArray dataArray = new JSONArray();
                dataArray.put(userData);
                data.putString("data", dataArray.toString());
            } catch (JSONException ex) {
                throw new FacebookException("Failed to construct request", ex);
            }
            GraphRequest request = new GraphRequest(AccessToken.getCurrentAccessToken(), String.format(Locale.US, "%s/user_properties", applicationID), data, HttpMethod.POST, callback);
            request.setSkipClientToken(true);
            request.executeAsync();
        }
    });
}
Also used : GraphRequest(com.facebook.GraphRequest) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers) Bundle(android.os.Bundle) FacebookException(com.facebook.FacebookException) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 4 with AttributionIdentifiers

use of com.facebook.internal.AttributionIdentifiers in project facebook-android-sdk by facebook.

the class FacebookSdk method publishInstallAndWaitForResponse.

static GraphResponse publishInstallAndWaitForResponse(final Context context, final String applicationId) {
    try {
        if (context == null || applicationId == null) {
            throw new IllegalArgumentException("Both context and applicationId must be non-null");
        }
        AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(context);
        SharedPreferences preferences = context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
        String pingKey = applicationId + "ping";
        String jsonKey = applicationId + "json";
        long lastPing = preferences.getLong(pingKey, 0);
        String lastResponseJSON = preferences.getString(jsonKey, null);
        JSONObject publishParams;
        try {
            publishParams = AppEventsLoggerUtility.getJSONObjectForGraphAPICall(AppEventsLoggerUtility.GraphAPIActivityType.MOBILE_INSTALL_EVENT, identifiers, AppEventsLogger.getAnonymousAppDeviceGUID(context), getLimitEventAndDataUsage(context), context);
        } catch (JSONException e) {
            throw new FacebookException("An error occurred while publishing install.", e);
        }
        String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
        GraphRequest publishRequest = GraphRequest.newPostRequest(null, publishUrl, publishParams, null);
        if (lastPing != 0) {
            JSONObject graphObject = null;
            try {
                if (lastResponseJSON != null) {
                    graphObject = new JSONObject(lastResponseJSON);
                }
            } catch (JSONException je) {
            // return the default graph object if there is any problem reading the data.
            }
            if (graphObject == null) {
                return GraphResponse.createResponsesFromString("true", null, new GraphRequestBatch(publishRequest)).get(0);
            } else {
                return new GraphResponse(null, null, null, graphObject);
            }
        } else {
            GraphResponse publishResponse = publishRequest.executeAndWait();
            // denote success since no error threw from the post.
            SharedPreferences.Editor editor = preferences.edit();
            lastPing = System.currentTimeMillis();
            editor.putLong(pingKey, lastPing);
            // if we got an object response back, cache the string of the JSON.
            if (publishResponse.getJSONObject() != null) {
                editor.putString(jsonKey, publishResponse.getJSONObject().toString());
            }
            editor.apply();
            return publishResponse;
        }
    } catch (Exception e) {
        // if there was an error, fall through to the failure case.
        Utility.logd("Facebook-publish", e);
        return new GraphResponse(null, null, new FacebookRequestError(null, e));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) JSONException(org.json.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers)

Example 5 with AttributionIdentifiers

use of com.facebook.internal.AttributionIdentifiers in project phonegap-facebook-plugin by Wizcorp.

the class Settings method publishInstallAndWaitForResponse.

static Response publishInstallAndWaitForResponse(final Context context, final String applicationId, final boolean isAutoPublish) {
    try {
        if (context == null || applicationId == null) {
            throw new IllegalArgumentException("Both context and applicationId must be non-null");
        }
        AttributionIdentifiers identifiers = AttributionIdentifiers.getAttributionIdentifiers(context);
        SharedPreferences preferences = context.getSharedPreferences(ATTRIBUTION_PREFERENCES, Context.MODE_PRIVATE);
        String pingKey = applicationId + "ping";
        String jsonKey = applicationId + "json";
        long lastPing = preferences.getLong(pingKey, 0);
        String lastResponseJSON = preferences.getString(jsonKey, null);
        // prevent auto publish from occurring if we have an explicit call.
        if (!isAutoPublish) {
            setShouldAutoPublishInstall(false);
        }
        GraphObject publishParams = GraphObject.Factory.create();
        publishParams.setProperty(ANALYTICS_EVENT, MOBILE_INSTALL_EVENT);
        Utility.setAppEventAttributionParameters(publishParams, identifiers, Utility.getHashedDeviceAndAppID(context, applicationId), getLimitEventAndDataUsage(context));
        publishParams.setProperty(AUTO_PUBLISH, isAutoPublish);
        publishParams.setProperty("application_package_name", context.getPackageName());
        String publishUrl = String.format(PUBLISH_ACTIVITY_PATH, applicationId);
        Request publishRequest = Request.newPostRequest(null, publishUrl, publishParams, null);
        if (lastPing != 0) {
            GraphObject graphObject = null;
            try {
                if (lastResponseJSON != null) {
                    graphObject = GraphObject.Factory.create(new JSONObject(lastResponseJSON));
                }
            } catch (JSONException je) {
            // return the default graph object if there is any problem reading the data.
            }
            if (graphObject == null) {
                return Response.createResponsesFromString("true", null, new RequestBatch(publishRequest), true).get(0);
            } else {
                return new Response(null, null, null, graphObject, true);
            }
        } else if (identifiers == null || (identifiers.getAndroidAdvertiserId() == null && identifiers.getAttributionId() == null)) {
            throw new FacebookException("No attribution id available to send to server.");
        } else {
            if (!Utility.queryAppSettings(applicationId, false).supportsAttribution()) {
                throw new FacebookException("Install attribution has been disabled on the server.");
            }
            Response publishResponse = publishRequest.executeAndWait();
            // denote success since no error threw from the post.
            SharedPreferences.Editor editor = preferences.edit();
            lastPing = System.currentTimeMillis();
            editor.putLong(pingKey, lastPing);
            // if we got an object response back, cache the string of the JSON.
            if (publishResponse.getGraphObject() != null && publishResponse.getGraphObject().getInnerJSONObject() != null) {
                editor.putString(jsonKey, publishResponse.getGraphObject().getInnerJSONObject().toString());
            }
            editor.apply();
            return publishResponse;
        }
    } catch (Exception e) {
        // if there was an error, fall through to the failure case.
        Utility.logd("Facebook-publish", e);
        return new Response(null, null, new FacebookRequestError(null, e));
    }
}
Also used : SharedPreferences(android.content.SharedPreferences) JSONException(org.json.JSONException) GraphObject(com.facebook.model.GraphObject) JSONException(org.json.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) JSONObject(org.json.JSONObject) AttributionIdentifiers(com.facebook.internal.AttributionIdentifiers)

Aggregations

AttributionIdentifiers (com.facebook.internal.AttributionIdentifiers)6 JSONException (org.json.JSONException)4 JSONObject (org.json.JSONObject)4 SharedPreferences (android.content.SharedPreferences)3 GraphObject (com.facebook.model.GraphObject)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Bundle (android.os.Bundle)1 FacebookException (com.facebook.FacebookException)1 GraphRequest (com.facebook.GraphRequest)1 JSONArray (org.json.JSONArray)1