Appending a GP template URL?

  • Hi,

    • I have a CPT called “positions” and contains maybe a dozen positions at our manufacturing facility.
    • Each (CPT)Position page is generated from a single Generate Press Template that contains a “Apply Now” button.
    • The templated position page then links to a single gateway page.
    • The Apply Now (right now)links to the URL of the gateway page with nothing else.
    • I would like to have the “position” carry thru the URL to the gateway page for tracking purposes. (an appended URL I guess)
    • The individual positions contain ACF fields – one of which is “position_name”(slug)

    I don’t have experience with this type of tracking and am curious what is the smartest approach when setting items like this up for GA4? I really don’t want to include extra plugins and such.

    Thank you for any help or suggestions.

  • Hi there,

    I don’t have experience with this type of tracking either. Can you gather more info about the tracking method and let us know what you want the dynamic tag to output?

  • Yes – let me get back to you. If I can figure it out – I will post the answer. That may spark a better idea for you that is better suited for generate Press.

  • Thanks, keep us updated!

  • <?php
    if ( is_singular( 'position' ) ) :
        $job_slug    = get_post_field( 'post_name', get_the_ID() );
        $position_id = get_the_ID();
    ?>
    <script>
    document.addEventListener('DOMContentLoaded', function () {
        var button = document.querySelector('.gb-button-370232e7');
        if (button) {
            var url = new URL(button.getAttribute('href'), window.location.origin);
            url.searchParams.set('job', '<?php echo esc_js( $job_slug ); ?>');
            url.searchParams.set('position_id', '<?php echo esc_js( $position_id ); ?>');
            button.setAttribute('href', url.toString());
        }
    });
    </script>
    <?php
    endif;
    ?>

    Code took site from hard-coded:
    /positions-at-heartland/job-application
    to
    /positions-at-heartland/job-application?job=material-handler&position_id=2575

    I also added a data attribute in my template – which i guess worked correctly:
    Data attribute: data-job | Value: {{post_slug}}

    I *think* this will work – any suggestions are helpful. Always concerned as to whether XYZ php code is safe to use.

  • The code looks solid, but better add a class to the button instead of using the button’s original class in this line var button = document.querySelector('.gb-button-370232e7');.

    As if you duplicate the button, the button’s class will change and the code would not work anymore.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.