Skip to main content
Transform your testing process with: Real Device Features, Company-wide Licences, & Test Observability

Cross Site Tracking on iOS Devices

This document guides you to toggle the ‘Cross-Site Tracking’ feature on iOS devices.

Introduction

Cross-site tracking takes place when you browse from one website to another website. You’re often followed by trackers that collect data on where you’ve been and what you’ve done, using scripts, widgets, etc. embedded on the sites you visit.

On iOS devices with iOS versions 11 and above the cross-site tracking is disabled by default. By setting the capability to false, you will basically be toggling the Prevent Cross-Site Tracking Safari setting to Off in the iPhone device. An image of the option in its default setting is shown below:

iPhone Prevent Cross Site Tracking Enabled

Sample code

If you are using BrowserStack SDK, you can set the following capability in the browserstack.yml file:

Copy icon Copy snippet

BrowserStack SDK is a plug-n-play solution that takes care of all the integration steps for you. Using the BrowserStack SDK is the recommended integration method for your project. To know more, visit the SDK core concepts page.

In case you want to enable cross-site tracking for testing purpose, you can do so by using BrowserStack’s preventCrossSiteTracking custom capability.

Capability Description Expected values
preventCrossSiteTracking Toggle cross-site tracking on iOS devices. A string. Default is true.

false if you want to enable cross-site tracking, true otherwise.

The following code snippet shows the usage of the capability preventCrossSiteTracking set to false which means that tracking would be enabled across sites i.e. cookies set in one site would be available in another site.

// Sample test in Java to run Automate session.
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class JavaSample {
	public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
	public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
	public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
	public static void main(String[] args) throws Exception {
	MutableCapabilities capabilities = new MutableCapabilities();
	HashMap<String, Object> browserstackOptions = new HashMap<String, Object>();
	browserstackOptions.put("osVersion", "14");
	browserstackOptions.put("deviceName", "iPhone 12");
	browserstackOptions.put("realMobile", "true");
	browserstackOptions.put("projectName", "BStack Sample Test");
	browserstackOptions.put("buildName", "BStack Build Number 1");
	browserstackOptions.put("preventCrossSiteTracking", "false");
	capabilities.setCapability("bstack:options", browserstackOptions);

		WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
		driver.get("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
		Thread.Sleep(10000);
		driver.quit();
	}
}
var webdriver = require('selenium-webdriver');
// Input capabilities
var capabilities = {
	'bstack:options' : {
		"osVersion" : "14",
		"deviceName" : "iPhone 12",
		"realMobile" : "true",
		"projectName" : "BStack Sample Test",
		"buildName" : "BStack Build Number 1",
		"preventCrossSiteTracking" : "false",
		"userName" : "YOUR_USERNAME",
		"accessKey" : "YOUR_ACCESS_KEY",
	},
	"browserName" : "safari",
}

var driver = new webdriver.Builder().usingServer('https://hub-cloud.browserstack.com/wd/hub').
withCapabilities(capabilities).build();
driver.get('https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html').then(function(){
      driver.quit();
});
using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver;
            SafariOptions capabilities = new SafariOptions();
            Dictionary<string, object> browserstackOptions = new Dictionary<string, object>();
            browserstackOptions.Add("osVersion", "14");
            browserstackOptions.Add("deviceName", "iPhone 12");
            browserstackOptions.Add("realMobile", "true");
            browserstackOptions.Add("projectName", "BStack Sample Test");
            browserstackOptions.Add("buildName", "BStack Build Number 1");
            browserstackOptions.Add("preventCrossSiteTracking", "false");
            browserstackOptions.Add("userName", "YOUR_USERNAME");
            browserstackOptions.Add("accessKey", "YOUR_ACCESS_KEY");
            capabilities.AddAdditionalOption("bstack:options", browserstackOptions);

            driver = new RemoteWebDriver(
              new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability
            );
            driver.Navigate().GoToUrl("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
            Thread.Sleep(10000);
            driver.Quit();
        }
    }
}
<?php
  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;
  $caps = array(
	'bstack:options' => array(
		"osVersion" => "14",
		"deviceName" => "iPhone 12",
		"realMobile" => "true",
		"projectName" => "BStack Sample Test",
		"buildName" => "BStack Build Number 1",
		"preventCrossSiteTracking" => "false",
	),
)

  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
  sleep(20)
  $web_driver->quit();
?>
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_cap = {
	'bstack:options' : {
		"osVersion" : "14",
		"deviceName" : "iPhone 12",
		"realMobile" : "true",
		"projectName" : "BStack Sample Test",
		"buildName" : "BStack Build Number 1",
		"preventCrossSiteTracking" : "false",
	},
}

driver = webdriver.Remote(
        command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
        desired_capabilities=desired_cap
    )
driver.get('https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html')
sleep(20)
driver.quit()
require 'rubygems'
require 'selenium-webdriver'
# Input capabilities
capabilities = {
	'bstack:options' => {
		"osVersion" => "14",
		"deviceName" => "iPhone 12",
		"realMobile" => "true",
		"projectName" => "BStack Sample Test",
		"buildName" => "BStack Build Number 1",
		"preventCrossSiteTracking" => "false",
	},
}

driver = Selenium::WebDriver.for(:remote,
  :url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.navigate.to "https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html"
sleep(20)
driver.quit

In case you want to enable cross-site tracking for testing purpose, you can do so by using BrowserStack’s browserstack.preventCrossSiteTracking custom capability.

Capability Description Expected values
browserstack.preventCrossSiteTracking Toggle cross-site tracking on iOS devices. A string. Default is true.

false if you want to enable cross-site tracking, true otherwise.

The following code snippet shows the usage of the capability browserstack.preventCrossSiteTracking set to false which means that tracking would be enabled across sites i.e. cookies set in one site would be available in another site.

// Sample test in Java to run Automate session.
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.URL;
public class JavaSample {
	public static final String AUTOMATE_USERNAME = "YOUR_USERNAME";
	public static final String AUTOMATE_ACCESS_KEY = "YOUR_ACCESS_KEY";
	public static final String URL = "https://" + AUTOMATE_USERNAME + ":" + AUTOMATE_ACCESS_KEY + "@hub-cloud.browserstack.com/wd/hub";
	public static void main(String[] args) throws Exception {
		DesiredCapabilities caps = new DesiredCapabilities();
		caps.setCapability("browserName", "iPhone");
		caps.setCapability("device", "iPhone 8 Plus");
		caps.setCapability("realMobile", "true");
		caps.setCapability("os_version", "12");
		caps.setCapability("name", "BStack-[Java] Sample Test"); // Test name
		caps.setCapability("build", "BStack Build Number 1"); // CI/CD job or build name
		caps.setCapability("browserstack.preventCrossSiteTracking", "false"); // This custom capability will enable cross site tracking
		WebDriver driver = new RemoteWebDriver(new URL(URL), caps);
		driver.get("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
		Thread.Sleep(10000);
		driver.quit();
	}
}
var webdriver = require('selenium-webdriver');
// Input capabilities
var capabilities = {
 'device' : 'iPhone 8 Plus',
 'realMobile' : 'true',
 'os_version' : '12',
 'browserName' : 'iPhone',
 'name': 'BStack-[NodeJS] Sample Test', // Test name
 'build': 'BStack Build Number 1', // CI/CD job or build name
 'browserstack.user' : 'YOUR_USERNAME',
 'browserstack.key' : 'YOUR_ACCESS_KEY',
 'browserstack.preventCrossSiteTracking': 'false' // This custom capability will enable cross site tracking
}
var driver = new webdriver.Builder().usingServer('https://hub-cloud.browserstack.com/wd/hub').
withCapabilities(capabilities).build();
driver.get('https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html').then(function(){
      driver.quit();
});
using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
namespace SeleniumTest
{
    class Program
    {
        static void Main(string[] args)
        {
            IWebDriver driver;
            OpenQA.Selenium.Safari.SafariOptions capability = new OpenQA.Selenium.Safari.SafariOptions();
            capability.AddAdditionalCapability("browserName", "iPhone");
            capability.AddAdditionalCapability("device", "iPhone 8 Plus");
            capability.AddAdditionalCapability("realMobile", "true");
            capability.AddAdditionalCapability("os_version", "12");
            capability.AddAdditionalCapability("name", "BStack-[C_sharp] Sample Test" ); // Test name
            capability.AddAdditionalCapability("build", "BStack Build Number 1"); // CI/CD job or build name
            capability.AddAdditionalCapability("browserstack.user", "YOUR_USERNAME");
            capability.AddAdditionalCapability("browserstack.key", "YOUR_ACCESS_KEY");
            capability.AddAdditionalCapability("browserstack.preventCrossSiteTracking", "false"); // This custom capability will enable cross site tracking

            driver = new RemoteWebDriver(
              new Uri("https://hub-cloud.browserstack.com/wd/hub/"), capability
            );
            driver.Navigate().GoToUrl("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
            Thread.Sleep(10000);
            driver.Quit();
        }
    }
}
<?php
  require_once('vendor/autoload.php');
  use Facebook\WebDriver\Remote\RemoteWebDriver;
  use Facebook\WebDriver\WebDriverBy;
  $caps = array(
    "browserName" => "iPhone",
    "device" => "iPhone 8 Plus",
    "realMobile" => "true",
    "os_version" => "12",
    "name" => "BStack-[Php] Sample Test", // Test name
    "build" => "BStack Build Number 1", // CI/CD job or build name
    "browserstack.preventCrossSiteTracking" => 'false' # This custom capability will enable cross site tracking
  );
  $web_driver = RemoteWebDriver::create(
    "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
    $caps
  );
  $web_driver->get("https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html");
  sleep(20)
  $web_driver->quit();
?>
from time import sleep
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_cap = {
 'browserName': 'iPhone',
 'device': 'iPhone 8 Plus',
 'realMobile': 'true',
 'os_version': '12',
 'name': 'BStack-[Python] Sample Test', # Test name
 'build': 'BStack Build Number 1', # CI/CD job or build name
 'browserstack.preventCrossSiteTracking': 'false' # This custom capability will enable cross site tracking
}
driver = webdriver.Remote(
        command_executor='https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub',
        desired_capabilities=desired_cap
    )
driver.get('https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html')
sleep(20)
driver.quit()
require 'rubygems'
require 'selenium-webdriver'
# Input capabilities
caps = Selenium::WebDriver::Remote::Capabilities.new
caps['device'] = 'iPhone 8 Plus'
caps['realMobile'] = 'true'
caps['os_version'] = '12'
caps['name'] = 'BStack-[Ruby] Sample Test' # Test name
caps['build'] = 'BStack Build Number 1' # CI/CD job or build name
caps['browserstack.preventCrossSiteTracking'] = 'false' # This custom capability will enable cross site tracking
driver = Selenium::WebDriver.for(:remote,
  :url => "https://YOUR_USERNAME:YOUR_ACCESS_KEY@hub-cloud.browserstack.com/wd/hub",
  :desired_capabilities => caps)
driver.navigate.to "https://alanhogan.github.io/web-experiments/3rd/third-party-cookies.html"
sleep(20)
driver.quit

You should now be able to toggle the ‘Prevent Cross-Site Tracking’ option on iOS device settings. If you need any further help, feel free to contact Support team.

This capability will NOT allow interacting with 3rd party iFrames in iOS Safari which is blocked due to CORS. This is a restriction from Apple and there are no workarounds currently. (See GitHub issue)

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback

Is this page helping you?

Yes
No

We're sorry to hear that. Please share your feedback so we can do better

Contact our Support team for immediate help while we work on improving our docs.

We're continuously improving our docs. We'd love to know what you liked






Thank you for your valuable feedback!

Talk to an Expert
Download Copy