ipunkt / laravel-analytics

Analytics tracking package for Laravel
MIT License
263 stars 54 forks source link

Proposal: disable JavaScript Template #28

Closed andreasba closed 7 years ago

andreasba commented 8 years ago

Hi there

thanks for this great package :-)

I have a suggestion: at the moment, if you

{!! Analytics::render() !!}

the package renders the whole thing, including the JavaScript tags:

<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','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-xxxxxxxxx-1', 'auto');
  ga('send', 'pageview');

</script>

My suggestion would be to provide an option to disable this, so that only the tracking code is rendered, like this:

  ga('create', 'UA-xxxxxxxxx-1', 'auto');
  ga('send', 'pageview');

The reason for this is that I want to comply with the EU cookie law and thus if the user does not accept cookies, I am not using Google Analytics. For example, I use CookieBar (a jQuery Plugin) like this:

            if(jQuery.cookieBar('cookies')){
                // Cookies are allowed
                // Lets set Google Analytics
                (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','//www.google-analytics.com/analytics.js','ga');

                {!! Analytics::render() !!}

                // Lets set Facebook
                (function(d, s, id) {
                    var js, fjs = d.getElementsByTagName(s)[0];
                    if (d.getElementById(id)) return;
                    js = d.createElement(s); js.id = id;
                    js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.4";
                    fjs.parentNode.insertBefore(js, fjs);
                }(document, 'script', 'facebook-jssdk'));
            }

Which of course does not work :-)

I think that this is a feature which a lot of people might already require or will require, so I thought I'd ask :-)

Thanks Andreas

rokde commented 8 years ago

Thanks for your issue. I implement that now.

andreasba commented 8 years ago

Sorry to re-open this @rokde but I manually set

Analytics::disableScriptBlock();

for every event or tracking like this

        // Lets fire off an GA event that a user has logged out
        Analytics::disableScriptBlock();
        Analytics::trackEvent('Account', 'Logout', $event->user->id);

But it still renders the JS block. Because in my layout I still have

{!! Analytics::render() !!}

So it seems like it doesnt work?

If I may, I would also suggest to make this available as a config setting too. I for example wouldnt have to set the disable roughly 40 times but only one then :-)

Thanks Andreas

rokde commented 8 years ago

Maybe the singleton problem with the facade. If you use it via dependency injection all works fine.

I'll check that soon. The config item is a really good advice. I will implement this too.

andreasba commented 8 years ago

Is there anything I can help you with (except code it, which I am not good enough to do :-)? I can test beta stuff if you like

rokde commented 8 years ago

Thank you @andreasba for your support. We have too much to do right now. So you can test the current code if the communicated problem is in all dependencies existent. At our tests nothing failed - so you can try to find the situation there this happened. Thanks in advance!

WaveHack commented 7 years ago

I made and PR'd a config variable for disabling script blocks by default.

Hope that resolves your issue @andreasba :)

rokde commented 7 years ago

This should solve your problem. Please re-test with version 1.3.4 @andreasba .

rokde commented 7 years ago

Please reopen an issue if it does not work for you @andreasba .