# Setting a form field from a cookie giving weird results

**URL:** <https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052>\
**Category:** Get Help\
**Tags:** cookies, google-analytics, gform\_field\_value, utm\
**Created:** [May 8, 2021, 12:32pm UTC](https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052 "2021-05-08T12:32:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5c8d2513a60013.5](https://avatars.discourse-cdn.com/v4/letter/u/8e7dd6/32.png) [@user5c8d2513a60013.5](https://community.gravityforms.com/u/user5c8d2513a60013.5)\
**Post date:** [May 8, 2021, 12:32pm UTC](https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052/1 "2021-05-08T12:32:18Z")

</div>

I am setting a Gravity form field value from a cookie (set by Google Tag Manager) using the filter below in functions.php

```auto
add_filter( 'gform_field_value_utm_campaign', 'populate_utm_campaign');
function populate_utm_campaign( $value) {
    return$_COOKIE['utm_campaign'];
}

```

All is working, however the values that are being populated are always from the previous session’s cookie (I’m always one session behind.)

I believe this might be due to the filter function running before Google Tag Manager gets a chance to set the new cookie.

I’m relatively new to this, so my question may need to be asked in a GTM forum, but I’m posting here in the hope someone might have had this problem before and be able to point me in the right direction.

---

<div class="post-metadata">

**Author:** ![pringletech](https://avatars.discourse-cdn.com/v4/letter/p/3da27b/32.png) [@pringletech](https://community.gravityforms.com/u/pringletech)\
**Post date:** [May 12, 2021, 11:54pm UTC](https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052/2 "2021-05-12T23:54:02Z")

</div>

Have a look at this plugin, I think it will help you out. I use it on a few sites with GF and it delivers as promised. A very simple and fast solution. I use the pro version.

> **[HandL UTM Grabber v3](https://utmgrabber.com/)**
>
> AI-Powered UTM Tracking & Lead Attribution for WordPress: Experience Superior Tracking with Our Top-Rated UTM Plugin. Effortlessly Monitor All Your Traffic Sources - Paid, Organic, Social, Referral, Direct - Right from Your WordPress Site. Say...

## PS - I am not an affiliate, just a user.

Scott

---

<div class="post-metadata">

**Author:** ![user5c8d2513a60013.5](https://avatars.discourse-cdn.com/v4/letter/u/8e7dd6/32.png) [@user5c8d2513a60013.5](https://community.gravityforms.com/u/user5c8d2513a60013.5)\
**Post date:** [May 13, 2021, 8:12am UTC](https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052/3 "2021-05-13T08:12:24Z")

</div>

Much appreciated, thank you. I’ll check it out.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [May 17, 2021, 5:39am UTC](https://community.gravityforms.com/t/setting-a-form-field-from-a-cookie-giving-weird-results/9052/4 "2021-05-17T05:39:01Z")

</div>


