sendCampaign

The sendCampaign() method associates campaign data with the next event sent by the SDK, allowing Piwik PRO to attribute that event to a campaign. To use this method, add campaign parameters to your campaign links and pass the resulting URL to the SDK.

The method itself doesn't send any event immediately. Instead, it store the campaign parameters and attaches them to the next event by the SDK, such as a screen view. After the event is sent, the campaign parameters are cleared.

Syntax

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    return PiwikTracker.sharedInstance()?.sendCampaign(url: url.absoluteString) ?? false
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    return [[PiwikTracker sharedInstance] sendCampaign:url.absoluteString];
}

Parameters

  • url (string, required)
    The campaign URL used to bring traffic to your mobile app.
    Any URL containing query parameters in the form ?key=value&key=value is accepted, including custom URL schemes used for deep links. Example: http://example.com?pk_campaign=Summer_Promo&pk_keyword=banking_app.

Examples

To pass campaign data from a campaign URL when the system delivers it to application(_:open:options:):

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
    return PiwikTracker.sharedInstance()?.sendCampaign(url: url.absoluteString) ?? false
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options {
    return [[PiwikTracker sharedInstance] sendCampaign:url.absoluteString];
}

Attach the .onOpenURL modifier to the root view of your App:

@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .onOpenURL { url in
                    PiwikTracker.sharedInstance()?.sendCampaign(url: url.absoluteString)
                }
        }
    }
}

Notes

  • The SDK forwards every key=value pair from the URL query string to Piwik PRO. The server determines which parameters are interpreted as campaign data.
  • Piwik PRO recognizes the standard pk_* and utm_* campaign parameters by default. For the full list of recognized parameters and instructions for adding custom parameters, see How can I customize Piwik PRO campaign parameters? or go to Menu > Administration > Sites & apps > Data collection > Campaigns.
  • The campaign URL must contain exactly one ?. URLs with multiple ? characters are ignored.

Related methods


Did this page help you?