Google Analytics for custom URL parameters

by Alok Tripathy   Last Updated October 31, 2017 16:04 PM

I want to track the stats for my website with custom GET parameters instead of utm parameters.

With UTM the URL gets longer and messy.

http://www.example.com/posts/scM21x?utm_source=fb&utm_medium=Social%20Media&utm_campaign=Facebook

What I would like is a URL like this

http://www.example.com/posts/scM21x?ref=fb

Then somehow tell Google Analytics to track the parameter ref=fb



Answers 3


You can set up Custom Dimensions in Google Analytics. First, I would recommend upgrading to Universal Analytics then do the following:

  1. Sign in to Google Analytics.

  2. Click Admin from the menu bar at the top of the page.

  3. From the dropdown menus, select an account and a property to which you want to add custom dimensions.

  4. Under the Property column, click Custom Definitions, then click Custom Dimensions.

  5. Click New Custom Dimension.

  6. Add a Name. This can be any string, but use something unique so it's not confused with any other dimension or metric in your reports.

  7. Select the Scope. Choose to track at the Hit, Session, User, or Product level. Read more about scope and how custom dimensions are processed in our Developer Guide.

  8. Check the Active box to start collecting data and see the dimension in your reports right away. To create the dimension but have it remain inactive, uncheck the box.

  9. Click Create.

Once you have your Custom Dimension created, you can use this function to send the hit to Google Analytics. This example uses PHP and you would need to create a Custom Dimension called ref.

<?php
    if(isset($_GET['ref']))
    { ?>
        <script>
            ga('send', 'pageview', {'ref':  '<?php echo $_GET["ref"]; ?>'});
        </script>
    <?php
    }
?>
Trey Copeland
Trey Copeland
January 07, 2015 20:40 PM

If you use Google Tag Manager then you can capture the value of the URL fragment and then match it with your Lookup Table macro. This is a much more efficient way of doing this.

1) Create a new Macro (Macro Type: URL -> Component Type: Fragment)

2) Create another Macro (Macro Type: Lookup Table)

a) enter all of your items such as fb, twitter, linkedin
b) use the macro from step one to match 'When {{macro1name}} equals'

3) Setup a new dimension in Google Analytics

4) Go to your Pageviews tag in GTM and under Custom Dimensions add your dimension index and the second micro.

dasickle
dasickle
January 07, 2015 21:23 PM

I found a way to add this GTM method directly into GA source and medium variables.

I applied the same GTM macros described above: URL Fragment and Lookup Table. I added a common start code ("cid") to each URL Fragment and that would go into table column 1. The column 2 would be the extended campaign description. Lookup table looks like this: 1) cid-fb1 / facebook_video-ads 2) cid-fb2 / facebook_link-ads...

I duplicated the GA tag and adjusted the firing rules: 1) One only fires when the URL Fragment starts with "cid" and replaces campaign source tracking for the Lookup Table value. 2) The generic GA tag was kept the same but one only firing when the "cid" URL Fragment is not there.

So a link http://example.com#cid-fb1 would replace campaign tracking and one without "#cid-fb1" it would not.

Paulo Fernandes
Paulo Fernandes
November 03, 2015 18:20 PM

Related Questions




Google Analytics problem with events

Updated April 10, 2017 14:04 PM


How to use segments in google analytics API

Updated May 13, 2015 12:39 PM