'Google Analytics 4 - persistent values are not sent when configured using the 'set' command

I want to configure an event parameter that will be sent automatically with every event I send to GA4. Including the page_view event and all other "Automatically collected events".

This is my setup configuration:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    
    gtag('js', new Date());

    // Doesn't work!!
    gtag('set', {
      'parameter_key': 'parameter_value'
    });

    gtag('config', 'G-XXXXXXXX');    
</script>

When I'm using the set command, the param_key event parameter is not sent with any event. It's totally ignored. However, when I move this object into the config command, it does work:

<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
<script>
    window.dataLayer = window.dataLayer || [];
    function gtag() { dataLayer.push(arguments); }
    
    gtag('js', new Date());

    // Works!
    gtag('config', 'G-XXXXXXXX', {
      'parameter_key': 'parameter_value'
    });
</script>

I want to use the set command because I have multiple properties, and I want this parameter to be used for all properties.

Anybody knows why it's not working and how to make it work?

Thanks!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source