Hubspot ESP integration with AppsFlyer

At a glance: Hubspot, an email delivery and analytics platform, embeds AppsFlyer OneLink links within emails. Email recipients clicking on the link are deep-linked into an app.

hubspot_and_appsflyer_logos.png

Email service providers (ESPs) are excellent sources of segmented user traffic. They wrap deep linking URLs with their own click recording domain. In some cases, doing so breaks iOS Universal Links. To overcome this, Hubspot uses Universal Links in addition to the click recording domain. They integrate with AppsFlyer and use OneLink branded link capabilities, enabling deep linking and click recording.

Set up Hubspot

Configure Hubspot and the AppsFlyer SDK, to send emails containing OneLink branded links. 

Prerequisites

Mobile SDK versions:

  • iOS: V4.9.0 or later 
  • Android: Currently not supported 

Step 1: Set up a Hubspot account

  1. Set up a Hubspot domain here.
  2. Set up hosting landing pages. Learn about this here
    Hubspot domains (example):

    sendios-create-project_en-us.png

Set up AppsFlyer

Step 2: Create OneLink

  1. Create a OneLink template for your email campaigns.
  2. Configure the app to support deep linking with OneLink.

Set up your app to support Universal Links

Step 3: Associate click domains in Xcode

  1. In Xcode, click your project.
  2. Click the Capabilities tab.
  3. Turn on Associated Domains, click the + sign, and add your click domain. For example, applinks:click.example.com.

    hubspot_xcode-capabilities_en-us.png

Step 4: Resolve deep links

  1. Installation: Add the AppsFlyerURLResolver to your project Podfile:  pod'AppsFlyerURLResolver'.
  2. Variable definition: Copy the code snippet below to your Xcode project
    var window: UIWindow?
    class AppDelegate: UIResponder, UIApplicationDelegate {
              var window: UIWindow? 
              varJSDeeplinkURL? // <<-- here we keep the hubspot link as a variable
  3. Resolve click domains: To get campaign details, the SDK needs to resolve the OneLink behind the click domain. The details are returned in the onAppOpenAttribution method. 
    • Define the click domains in the SDK property resolveDeepLinkURLs using the code below. Make sure to set this property before SDK initialization. 
      [[AppsFlyerTracker sharedTracker].resolveDeepLinkURLs = @[@"click.example.com",@"click.example.com"]];
    • Add the following code so that the SDK can resolve the click recording domain:
      (NSDictionary *)allHTTPHeaderFieldsForResolveDeepLinkURL:(NSURL *)URL {
      if ([URL.host isEqual: @"click.example.com"]) {
      return [NSDictionary dictionary];
      }
      else {
      return nil;
      }
      }
  4. Add the following code and define the domain that needs to be resolved, as highlighted in the following example:
    func application(_ application: UIApplication, continue userActivity: NSUserActivity,
    restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void) -> Bool {
    // This statement must include only the URL which will be resolved to JS script which inludes AppsFlyer URL
    if (userActivity.webpageURL?.absoluteString.contains(“click.example.com”) == true) {
    // This line will trigger URLResolver
    JSDeeplink = userActivity.webpageURL
    } else {
    // This is regular ESP links without JS resolving
    AppsFlyerLib.shared().continue(userActivity, restorationHandler: nil)
    }
    return true
    }
  5. To resolve the Hubspot URL, copy the code below:
    func applicationDidBecomeActive(_ application: UIApplication) {
    if let deeplink = JSDeeplink {
    JSDeeplink = nil
    URLResolver(isDebug: false).resolveJSRedirection(url: deeplink.absoluteString) { resolveURLStr in
    if let resolveURLStr = resolveURLStr,
    let resolveURL = URL(string: resolveURLStr) {
    AppsFlyerLib.shared().handleOpen(resolveURL, options: nil)
    }
    AppsFlyerLib.shared().start()
    }
    } else {
    AppsFlyerLib.shared().start()
    }
    }

Set up Hubspot deep linking

Step 5: Upload an AASA file to Hubspot

For Hubspot to support iOS Universal Links, an AASA file is required. The AASA file is automatically created and associated with OneLink when setting up OneLink with Universal Links. To obtain the AASA file and upload it to Hubspot:

  1. Under Experiences & Deep Linking in AppsFlyer, go to OneLink Management and find a OneLink link that's configured with Universal Links.
  2. Add /.well-known/apple-app-site-association to the end of the OneLink URL.
  3. Paste the OneLink link into your browser address bar and press Enter. For example, <OneLinkSubdomain>.onelink.me/.well-known/apple-app-site-association.
    The AASA file is downloaded to your computer and you can open it using any text editor.
  4. Upload the AASA file to Hubspot according to these guidelines.
    After uploading the AASA file, it appears under Files and folders:  

    hubsopt_upload-aasa_en-us.png

Step 6: Set up a URL redirection

Traffic from your domain that requires using an AASA file will be redirected to the Hubspot platform, where the file is located. The link address will include <yourdomain>/hubfs/….

To set up URL redirections, read here

  • The following example shows the URL redirection setup:

    hubspot_url-redirect-example_en-us.png

  • The following example shows a completed URL redirect:

    hubspot_url-redirection_en-us.png

For more information on setting up Hubspot, reach out to Hubspot support via the Hubsport platform.

Sending your first email

  1. Create a OneLink URL on the Link Management page (or manually). 
  2. When creating the OneLink URL, make sure to URL-encode all parameter values.
  3. The following parameters are highly recommended to use:
    • pid (media source) - Use a media source that signifies this usage such as Email.
    • c (campaign) - The name of the campaign you want to measure.
    • af_dp - The deep link URI scheme you want to deep link your users into.
    • af_web_dp - Where to redirect users clicking the link on the desktop.
    • af_ios_url - Where to redirect users that don’t have the app clicking the link on an iOS device.
    • af_android_url - Where to redirect users that don’t have the app click the link on an Android device.
  4. Place the OneLink URL in the email that you've created in Hubspot. For example:
    <a href="greatapp.onelink.me/abcd/1234567">Download my great app!</a>
    At this stage, Hubspot wraps the above link with the click domain that you set in the previous steps. Any click on the click domain redirects to the OneLink attribution link. If the app is installed on the user's device, the click domain deep-links into the app.

 Example

URL parameters are not URL encoded in this example, just for the sake of readability:

https://greatapp.onelink.me/abcd?pid=Email_Hubspot&c=Spring_Newsletter&is_retargeting=true&
	af_dp=testapp://path/to/content&af_web_dp=https://www.example.com/path/to/content&
	af_ios_url=https://www.example.com/path/to/content&af_android_url=https://www.example.com/path/to/content

Testing your links

After following all the required steps mentioned above, creating the appropriate OneLink URL, and placing it in an email, send yourself a test email.

What should you expect?

If you have the app installed, the app should be invoked. If the relevant AppsFlyer SDK deep linking APIs are implemented, you should be deep-linked into the relevant content.

In addition, a click should appear in both the AppsFlyer dashboard, attributed to the provided media source and campaign, and in Hubspot.