1

I need to be able to track UTM parameters in Google Analytics without them being in the URL.

We load information when the page loads, and we want to use this information in our UTM parameters, and we don't want to redirect users to the URL with UTM variables appended.

I see on this page you can call a pageview with javascript, but I don't see anyway to specify utm parameters: https://developers.google.com/analytics/devguides/collection/analyticsjs/pages.

Dibya Sahoo
  • 839
  • 3
  • 9
  • 30
LucyTurtle
  • 1,043
  • 1
  • 17
  • 37
  • Can you be more specific about what the URLs will look like? The purpose of UTM parameters is to track traffic's acquisition channels. By setting it once the user lands on the page, then you'll cause your tracking to be inaccurate and unnecessary sessions to be triggered. – XTOTHEL Apr 05 '19 at 03:40
  • Well the URLs will just be normal landing URLs from adsertisements so www.example.com/path/to/post. But once the user lands on the page we pull information about the post from our database, and therefore information that we'd like to send to analytics. – LucyTurtle Apr 05 '19 at 14:28
  • This is going to cause issues with GA. Also this isn’t UTM tracking. I don’t advise you doing it this way even if it is possible. Maybe try utilizing some custom dimensions. – XTOTHEL Apr 05 '19 at 17:04

1 Answers1

-1

I simply used the Google gtag.js documentation on PageViews to edit the script that loads the inital pageview information.

I first pull the current URL and append the variables to the end of the URL, and pass the new URL as the pageview to override the URL without the UTM parameters.

I am using laravel so @if statements will work within the JS, and {{}} is echoing the variables.

<script>
    var url = new URL( window.location.href );
    @if( isset( $request->utm_source ) )
        url.searchParams.set( 'utm_medium', '{{ strtolower( $request->utm_source ) }}' );
    @endif
    @if( isset( $request->utm_medium ) )
        url.searchParams.set( 'utm_medium', '{{ strtolower( $request->utm_medium ) }}' );
    @endif
    @if( isset( $request->utm_campaign ) )
        url.searchParams.set( 'utm_campaign', '{{ strtolower( $request->utm_campaign ) }}' );
    @endif
    @if( isset( $request->utm_term ) )
        url.searchParams.set( 'utm_term', '{{ strtolower( $request->utm_term ) }}' );
    @endif
    @if( isset( $request->utm_content ) )
        url.searchParams.set( 'utm_content', '{{ strtolower( $request->utm_content ) }}' );
    @endif

    function gtag() {
        dataLayer.push(arguments)
    }
    window.dataLayer = window.dataLayer || [], gtag("js", new Date), gtag("config", "GA_MEASUREMENT_ID", {
        'page_title': document.title,
        'page_location': url.href
    });
</script>
LucyTurtle
  • 1,043
  • 1
  • 17
  • 37
  • As I've stated in my comments, this is not a good solution. This will cause you to track everything as a campaign "unset" regardless if the user comes through a campaign or not, as well it will double count sessions since setting the campaign parameters causes GA to start a new session. – XTOTHEL Apr 05 '19 at 18:07
  • That isn't true. It's working just fine. I edited the initialization script instead of calling twice so it's only doing it once. – LucyTurtle Apr 05 '19 at 18:13
  • Okay, you can keep doing what you're doing. I've told you the risks and problems you are bringing onto yourself by utilizing your own solution for UTM. With this solution, you are going to be tracking everything that's not defined under the "unset" campaign which in turn will cause you to have decreased visibility in the actual source and mediums of all channels. – XTOTHEL Apr 05 '19 at 18:37
  • I'll just only set the caraibles when I've set them. We don't have the ability to do this the /right/ way. So doing this way is better than not having any of the information we need. Thank you for the warnings, but sometimes the "not the best way", is the best way in that situation. I understand the risks and problems and made the educated decision that this is the best way to provide the desired result for my bosses. – LucyTurtle Apr 05 '19 at 21:45