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.

Note. The path to configuration files may vary depending on your server settings.
sudo nano /etc/nginx/sites-enabled/default

Simple approach

Attention. 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.
  1. Install the lua module for nginx.

    sudo apt install libnginx-mod-http-lua
  2. 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 the sales.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:
  1. Open your website in Safari browser.
  2. In the menu, select Safari → Settings.
  3. In the Advanced tab, select Show features for web developers.
  4. Use the Cmd+Opt+I hotkey to switch to the developer mode and refresh the page.
  5. In the developer mode, select Network in the menu.
  6. Find the first request and check that the header is present in the Response field.