• Resolved sincewelastspoke

    (@sincewelastspoke)


    Hi,

    I set a Cookie (‘GCLID’) in my theme’s functions.php to store the Google Click ID.

    I then add this (using a RegEx) to the Cache Exclusions >> Cookies field in my Cache Enabler settings, however these cookies are not being set.

    /^(GCLID)_?/

    Why is this?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Anonymous User 16850768

    (@anonymized-16850768)

    If Cache Enabler delivers a cached page the functions.php file will not be processed. That means to set a cookie when Cache Enabler delivers a cached page this would need to be done in your server configuration file (e.g. .htaccess or nginx.conf).

    Thread Starter sincewelastspoke

    (@sincewelastspoke)

    Hi Corey,

    Thanks for getting back to me here ??

    Is there a tutorial available for achieving this? It would be a real ‘game changer’ for us at this end. Thank you.

    Anonymous User 16850768

    (@anonymized-16850768)

    You’re very welcome, @sincewelastspoke. I don’t have anything on hand for setting cookies in your server configuration file, but it would be done in the same way as setting any other response header.

    To point you in the right direction, can you please provide the snippet used in your functions.php file in addition to your server type (e.g. Apache or Nginx)?

    Thread Starter sincewelastspoke

    (@sincewelastspoke)

    Thank you so much ??

    I’m using Apache and here’s my function:

    function set_user_cookie() {
    	$gclid = $_GET['gclid'];
    		if (!isset($_COOKIE['GCLID'])) {
    			setcookie('GCLID', $gclid, time()+2678400, "/");
    		}
    }
    add_action( 'init', 'set_user_cookie');
    Anonymous User 16850768

    (@anonymized-16850768)

    Seeing that there is a conditional in what you’ve set, I’d actually recommend solving this with JavaScript instead, for example:

    const urlParams    = new URLSearchParams( window.location.search );
    const gclid        = urlParams.get( 'gclid' );
    const cookieExists = ( document.cookie.split( ';' ).some( ( item ) => item.trim().startsWith( 'gclid=' ) ) );
    
    if ( gclid && ! cookieExists ) {
        document.cookie = 'gclid=' + gclid + '; max-age=2678400; domain=example.com';
    }

    The above snippet will look for the gclid query parameter name. If it exists and the cookie is not set then a new cookie will be set with that query parameter value for 31 days (took the time from your snippet). example.com should be replaced with your actual domain.

    Hopefully that works for you. ??

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Exclude Cookie setting not working’ is closed to new replies.