Mobile

This entry provides detailed information on how to implement the On-Site Pixel in a mobile application environment.

Contents of this Page

Guidelines

This section provides specific code examples for the mobile app implementation of the On-Site Pixel in order to send the mobile device ID. It includes examples for the following languages: Swift, Objective-C, Android and Windows.

Swift
let advertisingId = UIDevice.currentDevice().identifierForVendor!.UUIDString;
let url = NSURL(string: String(format: "https://go.flx1.com/dp?_check=1&_nr=1&t=js&m=1&id=1&devid_a=%@", advertisingId));
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
	if let httpResponse = response as? NSHTTPURLResponse {
		print("status \(httpResponse.statusCode)")  // debug purposes
	}
}
task.resume()

Objective - C
NSString *advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSString *url = [NSString stringWithFormat:@"https://go.flx1.com/dp?_check=1&_nr=1&t=js&m=1&id=1&devid_a=%@", advertisingId];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:[NSURL URLWithString: url] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
	NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
	NSLog(@"response status code: %ld", (long)[httpResponse statusCode]);
}];
[dataTask resume];

Android
void send() {
	// Instantiate the RequestQueue.
	RequestQueue queue = Volley.newRequestQueue(this);

	String advertisingId = getAdvertisingId();
	if (advertisingId == null) {
		return;
	}

	Uri.Builder builder = new Uri.Builder();
	builder.scheme("https")
		.authority("go.flx1.com")
		.appendPath("px")
		.appendQueryParameter("_check", "1")
		.appendQueryParameter("_nr", "1")
		.appendQueryParameter("t", "js")
		.appendQueryParameter("m", "1")
		.appendQueryParameter("devid_g", advertisingId);
	String url = builder.build().toString();

	// Request a string response from the provided URL.
	StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
													new Response.Listener<String>() {

		@Override
		public void onResponse(String response) {
			Log.d("onResponse", "Response is: "+ response);
		}
	}, new Response.ErrorListener() {
		@Override
		public void onErrorResponse(VolleyError error) {
			Log.d("onErrorResponse", "Error");
		}
	});

	// Add the request to the RequestQueue.
	queue.add(stringRequest);
}

String getAdvertisingId() {
	String advertisingId = null;

	try {
		AdvertisingIdClient.Info adInfo = AdvertisingIdClient.getAdvertisingIdInfo(getBaseContext());
		advertisingId = adInfo.getId();
		boolean isLimitAdTrackingEnabled = adInfo.isLimitAdTrackingEnabled();
		Log.d("isLimitAdTracking", isLimitAdTrackingEnabled ? "y" : "n");
	} catch (Exception e) {
		e.printStackTrace();
	}

	return advertisingId;
}

/* USAGE */

 // We don't want to execute this in the main thread, getAdvertisingId doesn't allow to.
new Thread(new Runnable() {
	public void run() {
		try {
			send();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}).start();

Windows
/// <summary>
/// Get the Windows Advertising Id
/// </summary>
public static string GetWindowsAdId()
{
	return DeviceUtil.ReadWindowsAdvertisingId();
}