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 the _ym_uid is present in the Cookie request header, the server should send Set-Cookie with the same _ym_uid value and set it to expire in one year.
The same goes for _ym_d, which contains the _ym_uid creation time.
Instructions for nginx server
First, edit the nginx configuration file.
sudo nano /etc/nginx/sites-enabled/default
Simple approach
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
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
sudo nginx -t
sudo service nginx reload
- Open your website in Safari browser.
- In the menu, select Settings.
- In the Advanced tab, select Show features for web developers.
- 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.