4

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

I can set google analytics referrer manually with ga script. but I want to use gtag script for GA implementation. So How can I set the Referer manually using gtag script?

I tried this. but events are not hit with the referrer I set.

    <!-- Add gtag -->
    <!-- Global site tag (gtag.js) -->
    <script async src="https://www.googletagmanager.com/gtag/js"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());

        window.gtag('config', 'UA-160375581-4', {
            send_page_view: false,
        });
        
        window.gtag('set', { referrer: 'https://child.com' });

    </script>
Daniel Li
  • 170
  • 1
  • 12

1 Answers1

1

Do you need to use the gtag function? Can you load analytics.js and use the ga function?

<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

ga('create', 'UA-XXXXX-Y', 'auto');
ga('set', 'referrer', 'http://example.com');
ga('send', 'pageview');
</script>
<!-- End Google Analytics -->
Jayen
  • 5,653
  • 2
  • 44
  • 65