Kirby GA (Google Analytics) - Version 0.2

Kirby GA

Verion 0.1

Google Analytics for Kirby. Set it and forget it. Only Google Analytics ID required as a setting.

<?php echo ga(); ?>

Features

  • Load script only for not logged in users.
  • Load script only if not on a localhost enviroment.
  • Possible to disable the script completely.
  • Possible to debug and always load the script.
  • Possible to change the script template/snippet path.
  • Possible to change the IP blacklist.

Install

  1. Add ga folder in site/plugins/.
  2. Set Google Analytics id to your config.php. See options.
  3. Add <?php echo ga(); ?> in your footer snippet.

Options

ID (required)

The Google Analytics ID is required for the script to work.

c::set('plugin.ga.id', 'UA-12345678-12');

Active

You can deactivate the script by setting this value to false.

c::set('plugin.ga', true);

Debug

The script is only active for not logged in users that are not on a localhost environment. If you always want to have the code active, set this value to true.

c::set('plugin.ga.debug', false);

Template path

If you don’t like the script and want to change it, change the template path. You have access to $ua, $page, $pages and $site in the template.

c::set('plugin.ga.template', __DIR__ . DS . 'template.php');

$ua is the Google Analytics ID.

Blacklist

As default the localhost IP addresses are in the blacklist. That mean that the script will not run on these IP:s except if debug is turned on.

c::get('plugin.ga.blacklist', [ '127.0.0.1', '::1' ] );

Requirements

Kirby 2.3

License

MIT

Inpiration

I got inspired with the way KirbyAnalytics handle localhost enviroment.

5 Likes

Looks a lot like https://github.com/iksi/KirbyAnalytics
Good to have the debug option though

Thanks!

Yes, I got inspired. The main reason I built my own was that I never want to have the Google Analytics script executed when I’m logged in.

Then I added some extra options, because adding options is so simple in Kirby and it does not make the code much longer. :slight_smile:

2 Likes

Looks very cool !, i will test it :smiley: !

1 Like

Version 0.2 released

  • A snippet is used instead of a function in the footer.
  • Removed option plugin.ga.template.
  • Added option plugin.ga.snippet.
  • Adds <-- Kirby GA --> if script does not run (on localhost for example).

The main reason for this plugin is to try to never run Google Analytics when you build your site. The script will therefor not run for logged in users and users on localhost. There is an IP blacklist as well.

The repo name has changed

2 Likes

can you add a setting for anonymizeIp?

There are tons of things that can be added to the script and I can’t cover them all. Therefor I made it as a snippet with the latest version of the plugin.

You can override the snippet by simply create your own snippet in your snippets folder called ga. If you don’t like the snippet name you can change it in the config.php:

c::set('plugin.ga.snippet', 'ga');

The default snippet looks like this:

<?php if(ga::allowed()) : ?>
<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', '<?php echo c::get('plugin.ga.id'); ?>', 'auto');
  ga('send', 'pageview');
</script>
<?php else : ?>
<!-- Kirby GA -->
<?php endif; ?>

Is there a way to get some kind of dashboard stats like this :

with this plugin or any other workaround? Thank you in Advance

@gearsdigital is planning a GA dashboard widget, but I have no idea re. the current status of this.

1 Like

@novacanye What you’re looking for is a bit beyond my current target. I’m about to create a basic GA integration with only some dashboard widgets. An extended widget generator is not impossible but a lot of work… Let me think about it.

I’m planning a to provide a beta release in early May.

The installation instruction in the first post aren’t very clear and don’t match the instruction on GitHub.

What does that mean exactly? What ‘ga’ folder? Where do you get that ‘ga’ folder?

As you mentioned further down with your 0.2 release post, this code is now outdated, but anyone using Google or just searching the forum will more likely just read the first post.

The installation instructions in the GitHub repo are much clearer, but one also has to find them first.

Thanks for the plugin! :slight_smile:

For this and other plugins, I recommend to follow the original source, which for most plugins is a Github repository. This forum is a great way to promote and discuss plugins, but it’s not the original source.

Keeping the documentation updated in the forum means keeping two places up to date, the forum and the repository. Also, because the post is no longer editable after a specific time, I can’t keep it up to date without PM one of the crew members each time.

But because you had this problem, I’ve learned that we should probably never write installation instructions or details about the plugin in the forum, only more general features, images, videos but not exactly how to use them, because it might change over time.

In conclusion: Always go to the original source.

Hello! I am having trouble receiving any hits on the Google Analytics dashboard after I followed your Github tutorial.
Is <?php echo snippet('ga'); ?> what we are supposed to include in the footer snippet?

Thank you!

Yes, you have to include that snippet in the footer, no need to echo a snippet,though (although it says so in the plugin docs)

<?php snippet('ga'); ?>

But if you are on localhost, not logged in or haven’t added your GA id in the config, it won’t work.

1 Like

Hi, thanks for the help!
I am not on localhost, and I have added the GA id.

However I am a bit confused with the “logged in” part.
On the Github repo it says: Doesn't run the GA script if you are logged in.
In your response above you suggest I should be logged in, if I understand correctly.
Can you clarify?

Many thanks again!

Oh, sorry, the other way round, yes! Anyway, if you have added the snippet in the footer, what do you get on the page in dev tools?

Got it, thanks for the clarification.
There is no www.google-analytics.com folder in the dev tools Sources…which can’t be good I guess!

What I meant is if you see these two script tag in the footer in dev tools:

<script async="" src="https://www.google-analytics.com/analytics.js"></script>
<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', '', 'auto');
  ga('send', 'pageview');
</script>

Or <-- Kirby GA --> if script does not run.

No, I don’t. I’m pretty sure I’m not loading the pluggin correctly.
This is my footer.php file:

` <?php echo js("assets/js/jquery.min.js") ?>
<?php echo js("assets/js/underscore.js") ?>
<?php echo js("assets/js/main.js") ?>
<?php echo js("assets/js/jquery-ui/jquery-ui.js") ?>
<?php echo js("assets/js/classie.js") ?>
<?php echo js("assets/js/screensaver.js") ?>
<?php echo js("assets/js/isotope.js") ?>
<?php echo js("assets/js/modernizr.custom.js") ?>
<?php echo js("assets/js/jquery-zoom/jquery.zoom.js") ?>
<?= js('assets/plugins/embed/js/embed.js') ?>

<?php foreach($page->files()->filterBy('extension', 'js') as $js): ?>
<?php echo js($js->url()) ?>

<?php endforeach ?>
<?php echo snippet('ga'); ?>
`