Creating a new file download tag for Google Analytics

Hello,

after using Kirby for quite a while, I now want to track file downloads with Google Analytics. In principle this can be easily done with a short js command as shown here

In the kirby blog it it is pointed out, how to add new tags, but neither the wikipedia nor the mynewtag examples work in my system. Is this blog entry still showing an up to date version of how to add tags?

Is it possible to view the original functionality of the “file” tag and then copy it for a new modified tag?

Many thanks
Joscha

No, this blog post is deprecated, pls. checkout the docs here: https://getkirby.com/docs/advanced/kirbytext. And yes, you can copy an original tag, modify it for your purposes and either save it under a different name or overwrite the original tag.

Great, thanks for your support. The following code creates a new kirbytag “gafile” which implements a file download which creates a google analytics event so that file downloads can be tracked with google analytics…

<?php
kirbytext::$tags['gafile'] = array(
  'attr' => array(
    'text', 'popup', 'class'
  ),
  'html' => function($tag) {

    $file  = $tag->attr('gafile');
    $text  = $tag->attr('text', $file);
    $popup = $tag->attr('popup', 'no');
    $class = $tag->attr('class', '');
    
    //Tracking commands:
    // http://www.blastam.com/blog/index.php/2011/04/how-to-track-downloads-in-google-analytics
    if($popup == "no"){
        $target = '';
        $gacode = 'onclick="var that=this;ga(\'send\', \'event\', \'download\', \'file\',  this.href);setTimeout(function(){location.href=that.href;},200);return false;"';
        
    }else{
        $target = 'target="_blank"';
        $gacode = 'onclick="ga(\'send\', \'event\', \'download\', \'file\',  this.href);"';
    }

    return '<a href="' . $tag->page()->contentURL() . '/' . $file . '" ' . $target . ' ' . $gacode . ' class="' . $class . '">' . $text . '</a>';
  }
);
?>