Search code examples
javascriptgoogle-analytics

How to send medium and source with ga('send', 'pageview', ...)?


On some referral links (I cannot modify them), I didn't use the standard:

example.com/?utm_source=hey&utm_medium=mymedium&utm_campaign=campaign17

but I used the non-standard:

example.com/?src=foo

Thus, it is not tracked correctly in Google-Analytics. It is displayed as:

medium  source
(none)  (direct)

So I tried to pass the relevant parameters via a custom Javascript code:

ga('send', 'pageview', '/?utm_source=foo&utm_medium=test&utm_campaign=test');

instead of ga('send', 'pageview'); at the end of the standard analytics <script>...</script> snippet.

Still, it doesn't work: it is displayed as (none) (direct) (for example in Analytics' Realtime > Traffic sources).

Question: how to pass a source / medium / campaign name via ga('send', 'pageview', ...);?


Solution

  • You have to use ga('set', ...) command before ga('send', 'pageview'):

    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('set', 'campaignSource', 'foo');
    ga('set', 'campaignMedium', 'yourmedium');
    ga('set', 'campaignName', 'test');
    ga('send', 'pageview');
    

    Or you can use more simple syntax:

    ga('create', 'UA-XXXXX-Y', 'auto');
    ga('send', 'pageview', {'campaignSource': 'foo', 'campaignMedium': 'yourmedium', 'campaignName': 'test'});
    

    Field reference: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#trafficsources