General Campaign & Traffic Source Attribution
After an app has been installed, it may be launched by referrals from ad campaigns, websites, or other apps. In this scenario, referring traffic sources or marketing campaigns can be attributed to user activity in subsequent sessions by setting the campaign fields on a tracker directly.
The easiest way to send campaign data is to use [GAIDictionaryBuilder setCampaignParametersFromUrl:urlString], where urlString is a string representing a URL that may contain Google Analytics campaign parameters.
Note that in the following examples, campaign data is not set on the tracker directly, as it only needs to be sent once:
/*
* MyAppDelegate.m
* An example of how to implement campaign and referral attribution.
* If no Google Analytics campaign parameters are set in the referring URL, use the hostname as a referrer instead.
*/
// For iOS 9.0 and later
- (BOOL)application:(UIApplication *)app openURL:(nonnull NSURL *)url
options:(nonnull NSDictionary *)options {
// For iOS versions prior to 9.0
//- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
// sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSString *urlString = [url absoluteString];
id<GAITracker> tracker = [[GAI sharedInstance] trackerWithName:@"tracker" trackingId:@"UA-XXXX-Y"];
// setCampaignParametersFromUrl: parses Google Analytics campaign ("UTM")
// parameters from a string url into a Map that can be set on a Tracker.
GAIDictionaryBuilder *hitParams = [[GAIDictionaryBuilder alloc] init];
// Set campaign data on the map, not the tracker directly because it only needs to be sent once.
[hitParams setCampaignParametersFromUrl:urlString];
// Campaign source is the only required campaign field. If previous call did not set a campaign source, use the hostname as a referrer instead.
if(![hitParams get:kGAICampaignSource] && [url host].length !=0) {
// Set campaign data on the map, not the tracker.
[hitParams set:@"referrer" forKey:kGAICampaignMedium];
[hitParams set:[url host] forKey:kGAICampaignSource];
}
NSDictionary *hitParamsDict = [hitParams build];
// A screen name is required for a screen view.
[tracker set:kGAIScreenName value:@"screen name"];
// Previous V3 SDK versions.
// [tracker send:[[[GAIDictionaryBuilder createAppView] setAll:hitParamsDict] build]];
// SDK Version 3.08 and up.
[tracker send:[[[GAIDictionaryBuilder createScreenView] setAll:hitParamsDict] build]];
Alternatively, if you have campaign information in a form other than Google Analytics campaign parameters, you may set it on a NSDictionary and send it manually:
// Assumes at least one tracker has already been initialized.
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
// Note that it's not necessary to set kGAICampaignKeyword for this email campaign.
NSDictionary *campaignData = [NSDictionary dictionaryWithObjectsAndKeys:
@"email", kGAICampaignSource,
@"email_marketing", kGAICampaignMedium,
@"summer_campaign", kGAICampaignName,
@"email_variation1", kGAICampaignContent, nil];
// A screen name is required for a screen view.
[tracker set:kGAIScreenName value:@"screen name"];
// Note that the campaign data is set on the Dictionary, not the tracker. Previous V3 SDK versions.
// [tracker send:[[[GAIDictionaryBuilder createAppView] setAll:campaignData] build]];
// SDK Version 3.08 and up.
[tracker send:[[[GAIDictionaryBuilder createScreenView] setAll:campaignData] build]];