Rakesh
Rakesh

Reputation: 302

how to setup the app for receiving the utm parameters from url iOS?

I wanted the help to retrieve the utm parameters from the url the app was installed but the utm parameters are not received in the url. Can you guys please help in how to setup the app for receiving utm parameters from the app.

Here is the code I am doing to set the tracker and parameters

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]?) -> Bool
{
    if let gai = GAI.sharedInstance(){
        gai.tracker(withTrackingId: AppConfiguration.GOOGLE_ANALYTICS_ID)
        gai.defaultTracker.allowIDFACollection = true
    }
}

This method is called when app is installed from the url:-

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any]) -> Bool {

    let urlString = url.absoluteString

    let tracker = GAI.sharedInstance().defaultTracker

    // setCampaignParametersFromUrl: parses Google Analytics campaign ("UTM")
    // parameters from a string url into a Map that can be set on a Tracker.
    let hitParams = GAIDictionaryBuilder()

    // 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) != nil) && (url.host?.count ?? 0) != 0 {
        // Set campaign data on the map, not the tracker.
        hitParams.set("referrer", forKey: kGAICampaignMedium)
        hitParams.set(url.host, forKey: kGAICampaignSource)
    }
    //var hitParamsDict = hitParams.build()

    // A screen name is required for a screen view.
    tracker?.set(kGAIScreenName, value: "Sign up")
    var params = [AnyHashable : Any]()
    params[kGAICampaignMedium] = hitParams.get(kGAICampaignMedium)
    params[kGAICampaignSource] = hitParams.get(kGAICampaignSource)
    params[kGAICampaignContent] = hitParams.get(kGAICampaignContent)
    params[kGAICampaignName] = hitParams.get(kGAICampaignName)
    params[kGAICampaignKeyword] = hitParams.get(kGAICampaignKeyword)

    tracker?.send(params)

}

but the utm parameters are not received to the app. can you guys please help in this... Thanks in advance

Upvotes: 3

Views: 2171

Answers (1)

Dmitry
Dmitry

Reputation: 792

Have a look at the code below. Basically, you create params hastable, write there five (or less) parameters and call tracker.send with it. You are sending metadata, but not sending the actual hit (screenview or event or..).

// A screen name is required for a screen view.
tracker?.set(kGAIScreenName, value: "Sign up")
var params = [AnyHashable : Any]()
params[kGAICampaignMedium] = hitParams.get(kGAICampaignMedium)
params[kGAICampaignSource] = hitParams.get(kGAICampaignSource)
params[kGAICampaignContent] = hitParams.get(kGAICampaignContent)
params[kGAICampaignName] = hitParams.get(kGAICampaignName)
params[kGAICampaignKeyword] = hitParams.get(kGAICampaignKeyword)

tracker?.send(params)

The documentation here advices the following. Note the [GAIDictionaryBuilder createScreenView] call. I believe this is the reason why campaign data is not delivered

[tracker send:[[[GAIDictionaryBuilder createScreenView] setAll:campaignData] build]];

Upvotes: 3

Related Questions