• Stars
    star
    390
  • Rank 110,242 (Top 3 %)
  • Language
    Swift
  • License
    MIT License
  • Created over 12 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Matomo iOS, tvOS and macOS SDK: a Matomo tracker written in Swift

MatomoTracker (former PiwikTracker) iOS SDK

The MatomoTracker is an iOS, tvOS and macOS SDK for sending app analytics to a Matomo server. MatomoTracker can be used from Swift and Objective-C.

Fancy help improve this SDK? Check this list to see what is left and can be improved.

Installation

The MatomoTracker can be installed via CocoaPods, Carthage and the Swift Package Manager. In every file you want to use the MatomoTracker, don't forget to import the framework with import MatomoTracker.

CocoaPods

Use the following in your Podfile.

pod 'MatomoTracker', '~> 7.5'

Then run pod install.

Carthage

Carthage is a non intrusive way to install MatomoTracker to your project. It makes no changes to your Xcode project and workspace. Add the following to your Cartfile:

github "matomo-org/matomo-sdk-ios"
Swift Package Manager

You can use the Swift Package Manager as integration method. If you want to use the Swift Package Manager as integration method, either use Xcode to add the package dependency or add the following dependency to your Package.swift:

.package(url: "https://github.com/matomo-org/matomo-sdk-ios.git", from: "v7.5"),

Usage

Matomo Instance

The Matomo iOS SDK doesn't provide a instance of the PiwikTracker. In order to be able to track data you have to create an instance first.

let matomoTracker = MatomoTracker(siteId: "23", baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!)

The siteId is the ID that you can get if you add a website within the Matomo web interface. The baseURL it the URL to your Matomo web instance and has to include the "piwik.php" or "matomo.php" string.

You can either pass around this instance, or add an extension to the MatomoTracker class and add a shared instance property.

extension MatomoTracker {
    static let shared: MatomoTracker = MatomoTracker(siteId: "1", baseURL: URL(string: "https://example.com/piwik.php")!)
}

The siteId is the ID that you can get if you add a website within the Matomo web interface. The baseURL is the URL to your Matomo web instance and has to include the "piwik.php" or "matomo.php" string.

You can use multiple instances within one application.

Opting Out

The MatomoTracker SDK supports opting out of tracking. Please use the isOptedOut property of the MatomoTracker to define if the user opted out of tracking.

matomoTracker.isOptedOut = true

Tracking Page Views

The MatomoTracker can track hierarchical screen names, e.g. screen/settings/register. Use this to create a hierarchical and logical grouping of screen views in the Matomo web interface.

matomoTracker.track(view: ["path","to","your","page"])

You can also set the url of the page.

let url = URL(string: "https://matomo.org/get-involved/")
matomoTracker.track(view: ["community","get-involved"], url: url)

Tracking Events

Events can be used to track user interactions such as taps on a button. An event consists of four parts:

  • Category
  • Action
  • Name (optional, recommended)
  • Value (optional)
matomoTracker.track(eventWithCategory: "player", action: "slide", name: "volume", value: 35.1)

This will log that the user slid the volume slider on the player to 35.1%.

Tracking search

The MatomoTracker can track how users use your app internal search. You can track what keywords were searched for, what categories they use, the number of results for a certain search and what searches resulted in no results.

matomoTracker.trackSearch(query: "Best mobile tracking", category: "Technology", resultCount: 15)

Custom Dimension

The Matomo SDK currently supports Custom Dimensions for the Visit Scope. Using Custom Dimensions you can add properties to the whole visit, such as "Did the user finish the tutorial?", "Is the user a paying user?" or "Which version of the Application is being used?" and such. Before sending custom dimensions please make sure Custom Dimensions are properly installed and configured. You will need the ID of your configured Dimension.

After that you can set a new Dimension,

matomoTracker.set(value: "1.0.0-beta2", forIndex: 1)

or remove an already set dimension.

matomoTracker.remove(dimensionAtIndex: 1)

Dimensions in the Visit Scope will be sent along every Page View or Event. Custom Dimensions are not persisted by the SDK and have to be re-configured upon application startup.

Custom User ID

To add a custom User ID, simply set the value you'd like to use on the userId field of the tracker:

matomoTracker.userId = "coolUsername123"

All future events being tracked by the SDK will be associated with this userID, as opposed to the default UUID created for each Visitor.

Custom Visitor ID persisted on app starts

MatomoTracker will generate an _id upon first usage and will use this value to recognize the current visitor. This _id is persisted over app starts.

If you want to set your own visitor id, you can set your own visitor id with the forcedVisitorId field. Make sure you use a 16 character long hexadecimal string. The forcedVisitorId is persisted over app starts.

matomoTracker.forcedVisitorId = "0123456789abcdef"

Because the SDK persists this visitor id on app start, then we recommend to ask users for consent before tracking your app users.

Campaign Tracking

The Matomo iOS SDK supports campaign tracking.

matomoTracker.trackCampaign(name: "campaign_name", keyword: "campaign_keyword")

Content Tracking

The Matomo iOS SDK supports content tracking.

matomoTracker.trackContentImpression(name: "preview-liveaboard", piece: "Malaysia", target: "https://dummy.matomo.org/liveaboard/malaysia")
matomoTracker.trackContentInteraction(name: "preview-liveaboard", interaction: "tap", piece: "Malaysia", target: "https://dummy.matomo.org/liveaboard/malaysia")

Goal Tracking

The Matomo iOS SDK supports goal tracking.

matomoTracker.trackGoal(id: 1, revenue: 99.99)

Order Tracking

The Matomo iOS SDK supports order tracking.

let items = [
  OrderItem(sku: "product_sku_1", name: "iPhone Xs", category: "phone", price: 999.99, quantity: 1),
  OrderItem(sku: "product_sku_2", name: "iPhone Xs Max", category: "phone", price: 1199.99, quantity: 1)
]

matomoTracker.trackOrder(id: "order_id_1234", items: items, revenue: 2199.98, subTotal: 2000, tax: 190.98, shippingCost: 9)

Advanced Usage

Manual dispatching

The MatomoTracker will dispatch events every 30 seconds automatically. If you want to dispatch events manually, you can use the dispatch() function.

Session Management

The MatomoTracker starts a new session whenever the application starts. If you want to start a new session manually, you can use the startNewSession() function. You can, for example, start a new session whenever the user enters the application.

func applicationWillEnterForeground(_ application: UIApplication) {
  matomoTracker.startNewSession()
}

Logging

The MatomoTracker per default logs warning and error messages to the console. You can change the LogLevel.

matomoTracker.logger = DefaultLogger(minLevel: .verbose)
matomoTracker.logger = DefaultLogger(minLevel: .debug)
matomoTracker.logger = DefaultLogger(minLevel: .info)
matomoTracker.logger = DefaultLogger(minLevel: .warning)
matomoTracker.logger = DefaultLogger(minLevel: .error)

You can also write your own Logger and send the logs wherever you want. Just write a new class/struct and let it conform to the Logger protocol.

Custom User Agent

The MatomoTracker will create a default user agent derived from the WKWebView user agent. You can instantiate the MatomoTracker using your own user agent.

let matomoTracker = MatomoTracker(siteId: "5", baseURL: URL(string: "http://your.server.org/path-to-matomo/piwik.php")!, userAgent: "Your custom user agent")

Sending custom events

Instead of using the convenience functions for events and screen views for example you can create your event manually and even send custom tracking parameters. This feature isn't available from Objective-C.

func sendCustomEvent() {
  guard let matomoTracker = MatomoTracker.shared else { return }
  let downloadURL = URL(string: "https://builds.matomo.org/piwik.zip")!
  let event = Event(tracker: matomoTracker, action: ["menu", "custom tracking parameters"], url: downloadURL, customTrackingParameters: ["download": downloadURL.absoluteString])
  matomoTracker.track(event)
}

All custom events will be URL-encoded and dispatched along with the default Event parameters. Please read the Tracking API Documentation for more information on which parameters can be used.

Also: You cannot override Custom Parameter keys that are already defined by the Event itself. If you set those keys in the customTrackingParameters they will be discarded.

Automatic url generation

You can define the url property on every Event. If none is defined, the SDK will try to generate a url based on the contentBase of the MatomoTracker. If the contentBase is nil, no url will be generated. If the contentBase is set, it will append the actions of the event to it and use it as the url. Per default the contentBase is generated using the application bundle identifier. For example http://org.matomo.skd. This will not result in resolvable urls, but enables the backend to analyse and structure them.

Event dispatching

Whenever you track an event or a page view it is stored in memory first. In every dispatch run a batch of those events are sent to the server. If the device is offline or the server doesn't respond these events will be kept and resent at a later time. Events currently aren't stored on disk and will be lost if the application is terminated. #137

Contributing

Please read CONTRIBUTING.md for details.

License

MatomoTracker is available under the MIT license.

More Repositories

1

matomo

Liberating Web Analytics. Star us on Github? +1. Matomo is the leading open alternative to Google Analytics that gives you full control over your data. Matomo lets you easily collect data from websites & apps and visualise this data and extract insights. Privacy is built-in. We love Pull Requests!
PHP
18,186
star
2

device-detector

The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.
PHP
2,871
star
3

docker

Official Docker project for Matomo Analytics
Shell
799
star
4

referrer-spam-list

Community-contributed list of referrer spammers. Comment +1 in any issue or Pull request and the spammer will be added to the list!
652
star
5

matomo-nginx

Nginx configuration for running Matomo
406
star
6

matomo-sdk-android

SDK for Android to measure your apps with Matomo. Works on Android phones, tablets, Fire TV sticks, and more!
Java
388
star
7

matomo-log-analytics

Import any kind of server logs in Matomo for powerful log analytics. Universal log file parsing and reporting.
Python
208
star
8

matomo-php-tracker

PHP Client for Matomo Analytics Tracking API
PHP
188
star
9

tracker-proxy

HTTP proxy for Matomo's tracker API. This script allows to track websites with Matomo without revealing to your visitors the secret Matomo server URL.
PHP
145
star
10

tag-manager

Free Open Source Matomo Tag Manager - A simple way to manage and maintain all of your (third-party) tags on your website.
PHP
139
star
11

matomo-nodejs-tracker

A Node.js wrapper for the Matomo (Piwik) tracking HTTP API
JavaScript
116
star
12

matomo-for-wordpress

Get a fully functioning Matomo Analytics for your WordPress. Star us on Github? +1. Matomo is the leading open alternative to Google Analytics that gives you full control over your data. Privacy is built-in. 100% data ownership, no one else can see your data. We love Pull Requests!
PHP
108
star
13

matomo-mobile-2

Matomo Mobile 2 App - The official Git repository! Liberating Web Analytics on the go!
JavaScript
99
star
14

piwik-python-api

Piwik API for Python
Python
98
star
15

plugin-QueuedTracking

Scale your large traffic Matomo service by queuing tracking requests (in Redis or MySQL) for better performance.
PHP
81
star
16

piwik-dotnet-tracker

C# API client SDK for the Piwik Tracking API
C#
74
star
17

matomo-java-tracker

Official Java implementation of the Matomo Tracking HTTP API.
Java
69
star
18

searchengine-and-social-list

List of Search engines URLs, keywords and Social network URLs definitions used by Matomo Analytics
64
star
19

github-sync

Synchronize labels and milestones accross GitHub repositories
PHP
56
star
20

developer-documentation

Matomo Developer Website
PHP
54
star
21

component-ini

Read and write INI configurations.
PHP
49
star
22

github-issues-mirror

Provides a read-only mirror for your GitHub issues
PHP
41
star
23

plugin-LoginLdap

LDAP authentication and synchronization for Matomo.
PHP
38
star
24

piwik-dotnet-api

Official C# implementation of the Matomo Analytics API
C#
36
star
25

component-cache

PHP caching library based on Doctrine cache
PHP
34
star
26

matomo-dev-environment

Matomo Development Environment (Vagrant)
Puppet
33
star
27

github-changelog-generator

Queries the GitHub issues API to format a changelog
JavaScript
30
star
28

matomo-package

Matomo release script (official package), and debian/ubuntu package (allows sysadmins to deploy Matomo within seconds using "apt-get install piwik -V")
Makefile
28
star
29

plugin-MarketingCampaignsReporting

PHP
24
star
30

plugin-SecurityInfo

Provides security information about your PHP environment and offers suggestions based on PhpSecInfo from the PHP Security Consortium.
PHP
24
star
31

matomo-icons

Source files for the icons in Matomo
Python
22
star
32

plugin-GoogleAnalyticsImporter

Google Analytics to Matomo importer
PHP
22
star
33

component-network

Network component: manipulate IP addresses (ipv4, ipv6) in PHP used in the Matomo project
PHP
20
star
34

plugin-VisitorGenerator

Plugin to create fake visits, websites, users and goals to populate Matomo reports
PHP
19
star
35

travis-scripts

Scripts used to manage and automate travis CI builds for Matomo and plugins.
PHP
16
star
36

plugin-TreemapVisualization

Matomo plugin that lets you view reports as treemaps.
JavaScript
15
star
37

plugin-CustomDimensions

Custom Dimensions lets you track any custom data in Matomo
PHP
13
star
38

puppet-piwik

Piwik Puppet Module
Puppet
13
star
39

plugin-CustomAlerts

Alerts are a great way to get notified of changes on your website in Matomo.
PHP
13
star
40

plugin-Bandwidth

Monitor Bandwidth for each page, download, and measure overall traffic in bytes
PHP
13
star
41

matomo-map-generator

Python script that generates all svg maps used in the Matomo map widget
Python
12
star
42

component-decompress

Decompress files
PHP
12
star
43

plugin-EnvironmentVariables

Allows you to specify Matomo config in environment variables instead of the config file.
PHP
10
star
44

plugin-TrackingSpamPrevention

PHP
10
star
45

plugin-TasksTimetable

List all maintenance tasks that are scheduled to run. Displays the task names and next execution time in a table.
PHP
6
star
46

looker-studio-connector

The officially supported Matomo Connector for Looker Studio enables you to explore your Matomo data in Looker Studio at no cost. You can connect to both Matomo Cloud instances or On-Premise instances using just a Matomo Auth Token.
TypeScript
5
star
47

plugin-LogViewer

View your Matomo logs within Matomo
PHP
5
star
48

plugin-CustomVariables

This plugins allows you to configure and track Custom Variables in your Matomo Analytics.
PHP
5
star
49

plugin-Migration

Migrate a Matomo Measurable (website, app, roll-up, ...) from one Matomo instance to another Matomo
PHP
5
star
50

plugin-LoginHttpAuth

PLUGIN IS NOT ACTIVELY MAINTAINED - We're looking for a new maintainer to fork this plugin. see https://github.com/matomo-org/plugin-LoginHttpAuth/issues/26 --- Plugin to let you connect to Matomo using HTTP Auth protocol instead of the standard Login mechanism
PHP
5
star
51

plugin-Provider

Reports the Internet Service Provider of your visitors as part of your Matomo Analytics
PHP
4
star
52

plugin-AnonymousPiwikUsageMeasurement

Help making Matomo better by sending anonymized usage data to the creators the project, and/or to your own Matomo instance and/or to any other Matomo instance
PHP
4
star
53

matomo-wordpress-plugin-examples

Examples on how to enrich the Matomo Analytics for WordPress plugin.
PHP
3
star
54

plugin-DeviceDetectorCache

Makes tracking in Matomo Analytics faster by detecting many devices, operating systems, bots, and browsers from a cache.
PHP
3
star
55

plugin-PiwikDebugger

Debug and troubleshoot a Matomo server or a plugin with this useful plugin toolkit
PHP
3
star
56

matomo-marketplace-for-wordpress

Keep plugins from the Matomo Marketplace up to date in your WordPress with the convenience of a click. Get notified on new updates.
PHP
3
star
57

plugin-ClassicFontTheme

Less
2
star
58

cloudflare-app-manual

Piwik Analytics app for Cloudflare. Adds the tracking code when user already has a running Piwik.
JavaScript
2
star
59

piwik-python-tracker

Official Python implementation of the Piwik Tracking HTTP API.
2
star
60

github-action-plugin-translations

Github action for automatic translation update for plugins using transifex
Shell
2
star
61

plugin-IE11ReleaseChannel

Adds a new release channel that is still compatibile with Internet Explorer 11.
PHP
1
star
62

test-examples

PHP
1
star
63

google-tag-manager-matomo-template

Official Matomo Tracking Tag for Matomo Cloud & Google Tag Manager
Smarty
1
star
64

.github

1
star
65

code-search

Code search in Matomo and all Matomo plugins registered in the Marketplace
HTML
1
star
66

github-action-tests

tests package for github action
Shell
1
star
67

plugin-MatomoCustomRelease

PHP
1
star