Skip to content

4Analytics hooks

You can modify or extend 4Analytics behavior programmatically by using one or more of the hooks listed below. Hooks let you modify internal PHP structures or results computed by 4Analytics to better accomodate your website specific requirements.

They are events that are triggered at various moments by 4Analytics, providing code you write with internal information and possibly letting you modify this information.

Hooks are not altered when 4Analytics is updated

All the customization you do using hooks will survive 4Analytics updates. The hooks definition themselves will not change or be removed, unless the corresponding feature is modified or removed from 4Analytics of course.

When using hooks with 4Analytics, the following apply:

  • we cannot provide any form of support to the customization you make. You are on your own and the below documentation is the extent of information we'll provide to you.
  • you'll need suitable programming knowledge and understanding of Joomla internals to be able to write your own hooks handler
  • you may have to study 4Analytics code to learn if what you want to do is possible and how to do it. Again, we won't help your there, at least as part of regular support.

Usage

If you added any hook handler, make sure to absolutely always mention that whenever you open a support ticket. Your custom hooks can alter the way 4Analytics works, and may have consequences. We need to be aware of the existence of your hooks at all times.

Types

There are 2 types of hooks: filters and actions. Both are events that are triggered at different moment and to which you can respond.

The difference between them is:

  • filters: you receive some data, and you can return modified data. 4Analytics will subsequently use your modified data
  • action: you receive some data, but you cannot modify it. Or rather, you can modify it but 4Analytics will not use the result anyway

How to

As hooks are basically events, you must register your custom handler with 4Analytics for any hook you want to use. 4Analytics will then call your handler when the hook is run.

The most convenient way to register your hooks handler and include your custom code is to use 4Analytics functions file. The function file is unique and lives at:

/libraries/weeblr/foranalytics_functions.php

Create the file if it does not exist yet. You can put all your custom code in that file, or just use it to interface with 4Analytics and include more PHP code from wherever you want.

The 4Analytics function file must always start with:

<?php
/**
 * 4Analytics hooks file
 *
 * You can use 2 variables to access 4Analytics content:
 *
 * $factory: access variables
 * $hooks: add handlers
 */

// no direct access
defined('WBLIB_EXEC') || die;

If this preamble is missing, just copy/paste it at the top of the file.

After that, you can add your hook handler and your custom code. Here is an example that disable programatically the configuration rule that 4Analytics was about to use on the current page:

$hooks->add(
    'foranalytics_analytics_snippets',
    function ($ruleData) {
        return [];
    }
);

The $hooks variable shown in the example is always available to you inside the functions files, 4Analytics creates it for you.

Inside the functions file, you are inside of Joomla, meaning you can use Joomla API as you would in other contexts. You can for instance use the Joomla Factory class to access the application object. Make sure to add the proper namespace use statements if you do so.

API

404 Not Found

404 Not Found


nginx