setVisitorIdFrom
The setVisitorIdFrom methods read the pk_vid query parameter from a URL (string or URL) and, if it is valid, apply the same rules as setVisitorID to set the visitor ID. Call setVisitorIdFrom(deepLink:) or setVisitorIdFrom(url:) when the user opens your app from a link that carries pk_vid, for example a universal / app link opened in Safari or another browser on the same phone, or a link followed from an in-app web view. That way you can reuse the same visitor ID for the website (or web content) and the app on that phone.
Syntax
let stringResult = PiwikTracker.sharedInstance()?.setVisitorIdFrom(deepLink: "deepLink") ?? false
let urlResult = PiwikTracker.sharedInstance()?.setVisitorIdFrom(url: url) ?? falseBOOL result = [[PiwikTracker sharedInstance] setVisitorIdFromDeepLink:@"deepLink"];
BOOL result = [[PiwikTracker sharedInstance] setVisitorIdFromURL:url];Parameters
- deepLink
string, optional. Any URL string the system can parse as a URL; it must include apk_vidquery parameter whose first 16 characters are a valid visitor ID (lowercase hexadecimal0-9a-f, same rule asvisitorID). Example:piwik://example?pk_vid=25a3c7d060a94360. - url
URL, optional. Same requirements as deepLink, as anNSURL/URLvalue.
Returns
true if the visitor ID was updated from pk_vid. false if it was left unchanged: the URL is missing or cannot be parsed, pk_vid is absent, the value is shorter than 16 characters, or the first 16 characters are not valid lowercase hex (see visitorID).
Examples
To set the visitor ID from a deep link delivered to application(_:open:options:):
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool {
return PiwikTracker.sharedInstance()?.setVisitorIdFrom(url: url) ?? false
}- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options {
return [[PiwikTracker sharedInstance] setVisitorIdFromURL:url];
}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()?.setVisitorIdFrom(url: url)
}
}
}
}To set the visitor ID from a string-based deep link:
PiwikTracker.sharedInstance()?.setVisitorIdFrom(deepLink: "piwik://example?pk_vid=25a3c7d060a94360")[[PiwikTracker sharedInstance] setVisitorIdFromDeepLink:@"piwik://example?pk_vid=25a3c7d060a94360"];Notes
- If
pk_vidis longer than 16 characters, only the first 16 are used (they must still satisfy the hex format check). - For cross-platform flows, see Cross-platform tracking.
Related methods
Updated 15 days ago