> ## Documentation Index
> Fetch the complete documentation index at: https://memberful.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Integrate with Trackdesk

> Integrate Memberful with Trackdesk to run an affiliate program. Track referrals, attribute new member signups, and pay affiliates a commission on each sale.

export const RelatedDocs = ({link1, link2, link3, link4, link5, link6, link7, link8, link9, link10, className = ""}) => {
  const links = [link1, link2, link3, link4, link5, link6, link7, link8, link9, link10].filter(Boolean);
  if (!links.length) return null;
  return <section className={`related-docs border dark:border-gray-700 rounded-2xl px-6 py-4 mt-6 ${className}`}>
      <p className="mb-2 font-medium">
        <strong>Related help docs:</strong>
      </p>
      <ul className="space-y-1 mb-1 mt-1">
        {links.map((link, index) => <li key={index}>
            <a href={link.url}>{link.label}</a>
          </li>)}
      </ul>
    </section>;
};

In this help article, we'll integrate Trackdesk into Memberful to track and manage affiliate activity. For complete instructions on how to use Trackdesk, check out their [knowledge base.](https://help.trackdesk.com/en)

<Callout icon="info" color="#22E273">
  Memberful allows you to offer rewards to members who refer others to join your membership program in the form of a discount that will be applied to their active subscription. Learn more about our [built-in referrals program](/discounts-and-gifting/lifecycle-discounts/offer-referral-discounts/).
</Callout>

## Review your options for affiliate tracking

Memberful works best with [Tapfiliate](/discounts-and-gifting/affiliate-marketing/tapfiliate/) as your affiliate tracking software. However, you can also use Trackdesk, which is a more affordable option.

If you use Trackdesk, you'll need to manually prepare and add the affiliate tracking codes to your site, as there is no WordPress plugin for this service. Tapfiliate does have a WordPress plugin that automates this for you, so setting it up is much simpler.

One advantage of TrackDesk is that it's able to track renewals, refunds, and disputes automatically and without custom development required via their Stripe integration.

In this help article, we'll explore how to integrate Trackdesk with Memberful.

## Create your affiliate program

From your Trackdesk account, create an offer. We'll keep things simple for now so you can get up and running quickly.

Select **Custom built website** as the offer type, then select **Pay affiliates % from sales** or **Pay affiliates fixed commission from sales** depending on your preference.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/custom-built-website.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=1e96eff750d7a7af1b09309f4fb55612" alt="Custom built website" width="1440" height="794" data-path="images/integrations/affiliate-marketing/trackdesk/custom-built-website.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/percentage-or-fixed-commission.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=9aafd674fd361f69aa0bc8873fc03194" alt="Percentage or fixed commission" width="1440" height="564" data-path="images/integrations/affiliate-marketing/trackdesk/percentage-or-fixed-commission.png" />
</Frame>

Next, enter the URL of your Landing Page. This is where leads will arrive when they open the links that your affiliates will share.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/landing-page-url.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=e33ef9bcb848c3a94c6c6da6c547f0cf" alt="Landing Page URL" width="1440" height="813" data-path="images/integrations/affiliate-marketing/trackdesk/landing-page-url.png" />
</Frame>

Fill out the details of how much you'd like to pay your affiliates. You may see different fields depending on what you picked in previous steps.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/fill-out-payout-details.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=0acb8bda6169309ad02bca1b87742e02" alt="Fill out payout details" width="1440" height="1341" data-path="images/integrations/affiliate-marketing/trackdesk/fill-out-payout-details.png" />
</Frame>

Once you've created your offer, click the **Invite** button and copy the invite URL to share (on your website, via email, or through social media) so your affiliates can sign up.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-invite.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=9e5dd437e657964bfd7ef1f992e6fecd" alt="Trackdesk affiliate invite" width="1440" height="589" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-invite.png" />
</Frame>

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-invite-link.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=b74d94c50afba2c2cb77a52ab456265b" alt="Trackdesk affiliate invite link" width="1440" height="807" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-invite-link.png" />
</Frame>

Once a person signs up to become an affiliate, they will receive their own unique referral link to share with their customers or followers. They can find their unique link in the **Offers** section of their Trackdesk dashboard.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-unique-link.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=6095c9afc98d25c70f5813783ff4416c" alt="Trackdesk affiliate unique link" width="1440" height="539" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-affiliate-unique-link.png" />
</Frame>

## Integrate Trackdesk with Memberful

Although we'll be using Trackdesk for our affiliate tracking, we'll be using Memberful's Tapfiliate integration to make this work. Memberful's Tapfiliate integration is really a generic integration that can be used with other affiliate tracking software, although it was initially designed for Tapfiliate.

Go to *Revenue → Tapfiliate* in your Memberful dashboard, and click **Connect to Tapfiliate.**

Then go to *Website → External website,* and fill the **Redirect to this page after every order** field. If you prefer, you can also set this on a per-plan basis by going to the desired plan and setting the redirect URL.

This should be the URL of a page on your website, usually a "Thank you" page. Memberful-hosted pages like [our subscribe page](/website-builder/pages/subscribe-page/) or [plan purchase URLs](/plans/promote-and-sell/add-a-link-to-buy-a-plan) won't work.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-site-redirect-url.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=874c9e5f9165cdafef7f11f39b13abac" alt="Redirect URL" width="1440" height="1026" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-site-redirect-url.png" />
</Frame>

### Track conversions on your website

You'll need to add some JavaScript code to your pages to track visits to your website via affiliate links. Trackdesk calls these "clicks" but they're really visits to your website.

Place this code in every page of your website between the `<head>` and `</head>` HTML tags:

```javascript theme={null}
<!-- Trackdesk tracker begin -->
<script async src="//cdn.trackdesk.com/tracking.js"></script>
<script>
  (function(t,d,k){(t[k]=t[k]||[]).push(d);t[d]=t[d]||t[k].f||function(){(t[d].q=t[d].q||[]).push(arguments)}})(window,"trackdesk","TrackdeskObject");

  trackdesk("YOUR_TENANT_ID", "click");
</script>
<!-- Trackdesk tracker end -->
```

Replace **YOUR\_TENANT\_ID** with your actual Trackdesk account ID, which can be found in your Trackdesk account by going to *Settings → Your trackdesk identifiers → Tenant ID*.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-tenant-id.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=505ea4489aa834b30d193ce4e7ff31ca" alt="Trackdesk Tenant ID" width="1440" height="886" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-tenant-id.png" />
</Frame>

Next, you have two options:

1. If you want to issue one-time commissions for new orders and you don't need Trackdesk to automatically detect renewals, refunds, or disputes, you can insert some JavaScript code into your "Thank You" page. This code will report the conversion to Trackdesk.
2. However, if you want to issue recurring commissions to affiliates for the duration of the subscription, or you want Trackdesk to automatically react to refunds and disputes, you'll need to [integrate Trackdesk with Stripe](#integrate-trackdesk-with-stripe) and insert some more complex code into your "Thank You" page. This code will fetch the member's email address via Memberful's API so that Trackdesk and Stripe can continue to track future events for that member's subscription. Skip over to the [Integrate Trackdesk with Stripe](#integrate-trackdesk-with-stripe) section for instructions on how to do this.

If you picked option 1, place the following code inside the `<head>` tag of your "Thank You" page, configured in the [Integrate Trackdesk with Memberful](#integrate-trackdesk-with-memberful) section. This will fetch the member's email address via Memberful's API and report your conversion to Trackdesk:

```javascript theme={null}
<!-- Trackdesk tracker begin -->
<script async src="//cdn.trackdesk.com/tracking.js"></script>
<script>
  (function(t,d,k){(t[k]=t[k]||[]).push(d);t[d]=t[d]||t[k].f||function(){(t[d].q=t[d].q||[]).push(arguments)}})(window,"trackdesk","TrackdeskObject");

  trackdesk("YOUR_TENANT_ID", "conversion", {
    "conversionType": "sale",
    "externalId": {queryParam: "order_id"},
    "cid": {queryParam: "member_id"},
    "revenueOriginId": "YOUR_REVENUE_ORIGIN_ID",
    "amount": {
      "value": {queryParam: "amount"}
    }
  });
</script>
<!-- Trackdesk tracker end -->
```

Replace the **YOUR\_TENANT\_ID** and **YOUR\_REVENUE\_ORIGIN\_ID** values with your Trackdesk tenant ID and revenue origin ID, both of which can be found in your Trackdesk account by going to *Settings → Your trackdesk identifiers*.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/trackdesk-tenant-id-and-revenue-origin-id.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=fbe024c8910c3f06fb55cbdb5d9e31b3" alt="Tenant ID and Revenue Origin ID" width="1440" height="886" data-path="images/integrations/affiliate-marketing/trackdesk/trackdesk-tenant-id-and-revenue-origin-id.png" />
</Frame>

## Integrate Trackdesk with Stripe

Trackdesk integrates with Stripe, which allows Trackdesk to automatically detect refunds, disputes, and renewals (to pay your affiliates a recurring commission for the duration of a subscription). To integrate Trackdesk with Stripe:

1. From your Trackdesk dashboard, go to *Integrations → Stripe via Customer Email*.
2. Follow the first 2 steps on this page to integrate Trackdesk with Stripe. Ignore the last two steps for now.
3. The last two steps on this page instruct you to add some JavaScript code to your pages, but you can ignore that code. We've prepared some code for you here which is already adapted to work with Memberful.

Insert this code in the head section of every page of your website, as high as possible within the `<head>` and `</head>` HTML tags.

```javascript theme={null}
<!-- Trackdesk tracker begin -->
<script async src="//cdn.trackdesk.com/tracking.js"></script>
<script>
  (function(t,d,k){(t[k]=t[k]||[]).push(d);t[d]=t[d]||t[k].f||function(){(t[d].q=t[d].q||[]).push(arguments)}})(window,"trackdesk","TrackdeskObject");

  trackdesk("YOUR_TENANT_ID", "click");
</script>
<!-- Trackdesk tracker end -->
```

Next, you'll need to insert some code into the post-purchase page of your website, which is the page that your members see after they complete a purchase. This is usually a "Thank you" page.

In this code, we're going to make an API call to Memberful to fetch the member's email address, which we'll then pass to Trackdesk. Trackdesk will use this email address to track renewals, refunds, and disputes.

This API call should be made on the server-side, so you'll need to use a server-side language like PHP, because you don't want to expose your Memberful API key to the public.

If you're using WordPress, you can use a plugin like [WPCode](https://wordpress.org/plugins/insert-headers-and-footers/) to insert PHP code into your pages, although you should consult with a web developer to ensure that the code you insert is secure and won't break your website, and that you are handling any possible errors gracefully.

<Callout icon="info" color="#22E273">
  Our Tapfiliate integration doesn't require this step to track renewals (although refunds and disputes do require some extra work to track), so if you're looking for a simpler solution, use our [Tapfiliate integration](/discounts-and-gifting/affiliate-marketing/tapfiliate/#integrate-tapfiliate-with-stripe) instead.
</Callout>

Here is some sample PHP code to extract the member ID from the URL, query the member's data from Memberful's API, and report the member's email address to TrackDesk to track renewals, refunds, and disputes. This is a sample, though, so make sure to modify and expand the code accordingly.

```php theme={null}
<?php

$memberId = filter_input(INPUT_GET, 'member_id', FILTER_SANITIZE_NUMBER_INT);

if (!$memberId) {
    logError("memberID error: Invalid or missing memberId");
    return;
}

$responseData = executeGraphQlQuery($memberId);

if (!$responseData || !isset($responseData['data']['member']['email'])) {
    logError("Email not found in response");
    return;
}

$memberEmail = $responseData['data']['member']['email'];
$escapedEmail = htmlspecialchars($memberEmail, ENT_QUOTES, 'UTF-8');

outputTrackingScript($escapedEmail);

function executeGraphQlQuery($memberId) {
    $url = 'https://YOUR_MEMBERFUL_SUBDOMAIN.memberful.com/api/graphql';
    $authToken = "MEMBERFUL_API_KEY"; //Ideally, load this from an environment variable

    $query = <<<'GRAPHQL'
    query GetMember($id: ID!) {
        member(id: $id) {
            email
        }
    }
    GRAPHQL;

    $headers = [
        'Authorization: Bearer ' . $authToken,
        'Content-Type: application/json'
    ];

    $payload = json_encode([
        'query' => $query,
        'variables' => ['id' => (string)$memberId]
    ]);

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $payload,
        CURLOPT_HTTPHEADER => $headers,
        CURLOPT_SSL_VERIFYPEER => true
    ]);

    $response = curl_exec($ch);
    if (curl_errno($ch)) {
        logError('cURL error: ' . curl_error($ch));
        curl_close($ch);
        return null;
    }
    curl_close($ch);

    $responseData = json_decode($response, true);
    if (json_last_error() !== JSON_ERROR_NONE) {
        logError('JSON decode error: ' . json_last_error_msg());
        return null;
    }

    return $responseData;
}

function logError($message) {
    // Replace this with your chosen logging mechanism
    echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
}

function outputTrackingScript($email) {
    $emailJson = json_encode($email);
    echo <<<HTML
<!-- Trackdesk tracker begin -->
<script async src="//cdn.trackdesk.com/tracking.js"></script>
<script>
  (function(t,d,k){(t[k]=t[k]||[]).push(d);t[d]=t[d]||t[k].f||function(){(t[d].q=t[d].q||[]).push(arguments)}})(window,"trackdesk","TrackdeskObject");

  trackdesk("YOUR_TENANT_ID", "externalCid", {
    "externalCid": $emailJson,
    "revenueOriginId": "YOUR_REVENUE_ORIGIN_ID"
  });
</script>
<!-- Trackdesk tracker end -->
HTML;
}
?>
```

Take the following steps to adapt this code to your account:

* Replace **YOUR\_TENANT\_ID** and **YOUR\_REVENUE\_ORIGIN\_ID** with your Trackdesk tenant ID and revenue origin ID, both of which can be found in your Trackdesk account by going to *Settings → Your trackdesk identifiers*.
* Replace **YOUR\_MEMBERFUL\_SUBDOMAIN** with your actual Memberful Account URL subdomain, which can be found via *Settings → Website name and URL*.
* Replace **MEMBERFUL\_API\_KEY** with your actual Memberful API key, which can be found via *Settings → Custom applications* after you've [created a custom application](/api-reference/memberful-api/#using-the-graphql-api-explorer).

If you run into any issues, review [Trackdesk's documentation about integrating with Stripe](https://help.trackdesk.com/en/articles/7348516-stripe-integration-via-customer-email).

## Track and manage affiliate activity

If a member uses an affiliate's referral link to access your website and make a purchase, the information about the order is communicated to Trackdesk via the code you inserted into your website. You can manage your affiliates (and their payments) through your Trackdesk account.

### Locate a Trackdesk conversion in Memberful

If you did not integrate Trackdesk with Stripe, you can locate a Trackdesk conversion in Memberful by replacing some values in the URL below and opening it in your browser:

```text theme={null}
https://YOUR_MEMBERFUL_SUBDOMAIN.memberful.com/admin/orders/YOUR_ORDER_ID
```

Replace **YOUR\_MEMBERFUL\_SUBDOMAIN** with your actual Memberful Account URL subdomain, which can be found via *Website → Settings.*

Replace **YOUR\_ORDER\_ID** with the *External ID* for this conversion in Trackdesk. You'll probably need to scroll all the way to the right to see this column in the table of conversions.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/conversion-external-id.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=29604acf02209200ea80ca3051e94847" alt="Conversion External ID" width="1440" height="488" data-path="images/integrations/affiliate-marketing/trackdesk/conversion-external-id.png" />
</Frame>

If you integrated Trackdesk with Stripe, conversions are detected automatically in Stripe, so the Memberful Order ID isn't present in Trackdesk. However, you can still locate the order by searching for the member's email address (displayed in the "External CID" column in Trackdesk) in Memberful and looking for an order placed by that member with an amount and date that matches the conversion in Trackdesk. The date might be off by 1 day due to time zone differences.

<Frame>
  <img src="https://mintcdn.com/memberful/8cnaMPD_oPIjL0By/images/integrations/affiliate-marketing/trackdesk/conversion-external-cid.png?fit=max&auto=format&n=8cnaMPD_oPIjL0By&q=85&s=17ecf0f7f6128cf34adfb23d3c9fc623" alt="Conversion External CID" width="1440" height="498" data-path="images/integrations/affiliate-marketing/trackdesk/conversion-external-cid.png" />
</Frame>

### Set up recurring commissions

If you've integrated your Trackdesk account with Stripe and implemented the [conversion tracking script](#track-conversions-on-your-website), Trackdesk will automatically track recurring conversions for you, as it will be able to identify a returning member by their email address.

### Automatically handle refunds

If you've integrated your Trackdesk account with Stripe and implemented the [conversion tracking script](#track-conversions-on-your-website), all refunds initiated inside Memberful will automatically be propagated to your Trackdesk account.

Full refunds will result in a commission being disapproved, while partial refunds will decrease the refunded amount from the total conversion amount.

### Automatically handle disputes

If you've integrated your Trackdesk account with Stripe and implemented the [conversion tracking script](#track-conversions-on-your-website), all disputes in Stripe will automatically result in a Trackdesk commission being disapproved.

<RelatedDocs
  link1={{
url: "/discounts-and-gifting/affiliate-marketing/tapfiliate/",
label: "Integrate Memberful with Tapfiliate.",
}}
  link2={{
url: "/discounts-and-gifting/track-member-referrals/",
label: "Track member referrals.",
}}
  link3={{
url: "/metrics-and-analytics/tracking/integrate-with-google-analytics/",
label: "Integrate with Google Analytics.",
}}
  link4={{
url: "/metrics-and-analytics/tracking/add-custom-conversion-tracking/",
label: "Add custom conversion tracking.",
}}
/>
