1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/ReferrerReceiver.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,62 @@ 1.4 +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +package org.mozilla.gecko; 1.10 + 1.11 +import org.json.JSONException; 1.12 +import org.json.JSONObject; 1.13 + 1.14 +import android.content.BroadcastReceiver; 1.15 +import android.content.Context; 1.16 +import android.content.Intent; 1.17 +import android.util.Log; 1.18 + 1.19 +import java.net.URLDecoder; 1.20 +import java.util.HashMap; 1.21 + 1.22 +public class ReferrerReceiver 1.23 + extends BroadcastReceiver 1.24 +{ 1.25 + private static final String LOGTAG = "GeckoReferrerReceiver"; 1.26 + 1.27 + public static final String ACTION_INSTALL_REFERRER = "com.android.vending.INSTALL_REFERRER"; 1.28 + public static final String UTM_SOURCE = "mozilla"; 1.29 + 1.30 + @Override 1.31 + public void onReceive(Context context, Intent intent) { 1.32 + if (ACTION_INSTALL_REFERRER.equals(intent.getAction())) { 1.33 + String referrer = intent.getStringExtra("referrer"); 1.34 + if (referrer == null) 1.35 + return; 1.36 + 1.37 + HashMap<String, String> values = new HashMap<String, String>(); 1.38 + try { 1.39 + String referrers[] = referrer.split("&"); 1.40 + for (String referrerValue : referrers) { 1.41 + String keyValue[] = referrerValue.split("="); 1.42 + values.put(URLDecoder.decode(keyValue[0]), URLDecoder.decode(keyValue[1])); 1.43 + } 1.44 + } catch (Exception e) { 1.45 + } 1.46 + 1.47 + String source = values.get("utm_source"); 1.48 + String campaign = values.get("utm_campaign"); 1.49 + 1.50 + if (source != null && UTM_SOURCE.equals(source) && campaign != null) { 1.51 + try { 1.52 + JSONObject data = new JSONObject(); 1.53 + data.put("id", "playstore"); 1.54 + data.put("version", campaign); 1.55 + 1.56 + // Try to make sure the prefs are written as a group 1.57 + GeckoEvent event = GeckoEvent.createBroadcastEvent("Campaign:Set", data.toString()); 1.58 + GeckoAppShell.sendEventToGecko(event); 1.59 + } catch (JSONException e) { 1.60 + Log.e(LOGTAG, "Error setting distribution", e); 1.61 + } 1.62 + } 1.63 + } 1.64 + } 1.65 +}