How can I add a UTM tag with PHP code to pick up the post slug dynamically in the UTM?

What comes to mind is getting the current post by global $post, next getting the post slug by $post->post_name, next checking the slug if it needs to update a link with a utm query.

So something like this.

// get the current post
global $post;

// get the post slug, if not set then set it to empty string
$post_slug = $post->post_name ?? '';

// an array of slugs that need to add utm to link
$utm_slugs = [
    'slug-1',
    'slug-2',
    'slug-3'
];

// set default value (empty string), in case no slug was matched
$utm_query = '';

// if post slug exists and in the utm slugs array, set a utm query
if (!empty($post_slug) && in_array($post_slug, $utm_slugs)) $utm_query = '?utm_source=" . $post_slug;

Now that we have the php part taken care of we need to create the link.
This is just an example so change it accordingly.

<a href="http://domain.com/<?= $utm_query; ?>">Some link</a>

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)