Setting up the server to support Safari
To enhance data collection and ad targeting in the Safari browser, set up the _ym_uid
cookie update on the server. This type of cookie helps to identify your site users.
Cookie update logic
If a Cookie
request header contains _ym_uid
, the server must send Set-Cookie
with the same value (_ym_uid
) and set its expiration to 1 year.
The same goes for _ym_d
, which contains the _ym_uid
creation time.
Instructions for nginx server
First, edit the nginx configuration file.
Note
The path to configuration files may vary depending on your server settings.
sudo nano /etc/nginx/sites-enabled/default
Simple approach
Alert
Keep in mind that you can't use this approach if you use the add_header
directive to set other headers or the if
directive for conditional routing. To avoid undesired behavior in such scenarios, use the advanced approach.
Add the following code to each location
block (anywhere within the block):
Note
If your service does not use the HTTPS protocol, remove the Secure;
parameter from the header.
If your service uses subdomains, use the root domain instead of the $host
variable (e.g., example.com
for the sales.example.com
site).
# Update Yandex Metrica cookies
if ($cookie__ym_uid) {
set $ym_postfix "Max-Age=31536000;Secure;Path=/;Domain=.$host";
add_header Set-Cookie "_ym_uid=$cookie__ym_uid;$ym_postfix";
add_header Set-Cookie "_ym_d=$cookie__ym_d;$ym_postfix";
add_header Set-Cookie "_ym_ucs=nginx;$ym_postfix";
}
Advanced approach
Note
Your server might use a different package name and its installation method.
-
Install the
lua
module for nginx.sudo apt install libnginx-mod-http-lua
-
Add the following code to each
server
block (anywhere within the block):Note
If your service does not use the HTTPS protocol, remove the
Secure;
parameter from the header.If your service uses subdomains, use the root domain enclosed in quotes instead of the
ngx.var.host
variable (e.g.,example.com
for thesales.example.com
site).# Update Yandex Metrica cookies header_filter_by_lua_block { if ngx.var.cookie__ym_uid and ngx.var.host then local ym_postfix = "Max-Age=31536000; Secure; Path=/; Domain=." .. ngx.var.host ngx.header["Set-Cookie"] = { "_ym_uid=" .. ngx.var.cookie__ym_uid .. "; " .. ym_postfix, "_ym_d=" .. (ngx.var.cookie__ym_d or "") .. "; " .. ym_postfix, "_ym_ucs=nginx; " .. ym_postfix } end }
Final steps
Test the new configuration:
sudo nginx -t
Once the test is successful, apply the nginx configuration:
sudo service nginx reload
To check whether the server is configured correctly:
- Open your website in Safari browser.
- In the menu, select Safari → Settings.
- In the Advanced tab, check the Show features for web developers box.
- Use the
Cmd+Opt+I
hotkey to switch to the developer mode and refresh the page. - In the developer mode, select Network in the menu.
- Find the first request and check that the header is present in the Response field.
Useful links |
Online training |