Class MatomoTracker

java.lang.Object
org.matomo.java.tracking.MatomoTracker
All Implemented Interfaces:
AutoCloseable

public class MatomoTracker extends Object implements AutoCloseable
The main class that sends MatomoRequests to a specified Matomo server.

Contains several methods to send requests synchronously and asynchronously. The asynchronous methods return a CompletableFuture that can be used to wait for the request to finish. The synchronous methods block until the request is finished. The asynchronous methods are more efficient if you want to send multiple requests at once.

Configure this tracker using the TrackerConfiguration class. You can use the TrackerConfiguration.builder() to create a new configuration. The configuration is immutable and can be reused for multiple trackers.

The tracker is thread-safe and can be used by multiple threads at once.

Author:
brettcsorba
  • Constructor Details

    • MatomoTracker

      public MatomoTracker(@NonNull @NonNull TrackerConfiguration trackerConfiguration)
      Creates a new Matomo Tracker instance.
      Parameters:
      trackerConfiguration - Configurations parameters (you can use a builder)
  • Method Details

    • sendRequest

      public void sendRequest(@NonNull @NonNull MatomoRequest request)
      Sends a tracking request to Matomo using the HTTP GET method.

      Use this method if you want to send a single request. If you want to send multiple requests at once, use sendBulkRequest(Iterable) instead. If you want to send multiple requests asynchronously, use sendRequestAsync(MatomoRequest) or sendBulkRequestAsync(Collection) instead.

      Parameters:
      request - request to send. must not be null
    • sendRequestAsync

      public CompletableFuture<MatomoRequest> sendRequestAsync(@NonNull @NonNull MatomoRequest request)
      Send a request asynchronously via HTTP GET.

      Use this method if you want to send a single request. If you want to send multiple requests at once, use sendBulkRequestAsync(Collection) instead. If you want to send multiple requests synchronously, use sendRequest(MatomoRequest) or sendBulkRequest(Iterable) instead.

      Parameters:
      request - request to send
      Returns:
      completable future to let you know when the request is done. Contains the request.
    • sendBulkRequest

      public void sendBulkRequest(MatomoRequest... requests)
      Send multiple requests in a single HTTP POST call.

      More efficient than sending several individual requests. If you want to send a single request, use sendRequest(MatomoRequest) instead. If you want to send multiple requests asynchronously, use sendBulkRequestAsync(Collection) instead.

      Parameters:
      requests - the requests to send
    • sendBulkRequest

      public void sendBulkRequest(@NonNull @NonNull Iterable<? extends MatomoRequest> requests)
      Send multiple requests in a single HTTP POST call.

      More efficient than sending several individual requests. If you want to send a single request, use sendRequest(MatomoRequest) instead. If you want to send multiple requests asynchronously, use sendBulkRequestAsync(Collection) instead.

      Parameters:
      requests - the requests to send
    • sendBulkRequestAsync

      public CompletableFuture<Void> sendBulkRequestAsync(MatomoRequest... requests)
      Send multiple requests in a single HTTP call. More efficient than sending several individual requests.
      Parameters:
      requests - the requests to send
      Returns:
      completable future to let you know when the request is done
    • sendBulkRequestAsync

      public CompletableFuture<Void> sendBulkRequestAsync(@NonNull @NonNull Collection<? extends MatomoRequest> requests)
      Send multiple requests in a single HTTP call. More efficient than sending several individual requests.
      Parameters:
      requests - the requests to send
      Returns:
      completable future to let you know when the request is done
    • close

      public void close() throws Exception
      Specified by:
      close in interface AutoCloseable
      Throws:
      Exception
    • setSenderFactory

      protected void setSenderFactory(SenderFactory senderFactory)