49°F

Aaron Parecki

  • Articles
  • Notes
  • Photos
  • affiliate-links.php

    February 12, 2022
    1. <?php /*
    2.  
    3. Affiliate Link Mapping
    4. ======================
    5.  
    6. Map affiliate links from the main landing page to individual affiliate links
    7. for specific stores.
    8.  
    9. Usage
    10. -----
    11.  
    12. Define your affiliate link mapping in the `$AFFILIATES` array. The top-level
    13. keys are your own affiliate links you make up for your main site. Each
    14. is an array listing out the query string parameters for the other stores
    15. affiliate programs.
    16.  
    17. ```
    18. $AFFILIATES = [
    19.   'user1' => [
    20.   'store1' => ['p' => '1'],
    21.   'store2' => ['aff' => 'y2'],
    22.   'store3' => ['aff' => '1'],
    23.   'store4' => ['tag' => 'hello-20'],
    24.   ],
    25.   'user2' => [
    26.   'store3' => ['aff' => 'y3'],
    27.   ],
    28.   'user3' => [
    29.   'store1' => ['p' => '3', 'q' => '4'],
    30.   ],
    31.   'user4' => [
    32.   'store2' => ['aff' => 'y8'],
    33.   'store3' => ['aff' => '4'],
    34.   ],
    35. ];
    36. ```
    37.  
    38. In this example, we create 4 new affiliate codes `user1`, `user2`, etc.
    39. There are also 4 stores defined, each with their own different set of
    40. affiliate link parameters.
    41.  
    42. The links from your main website, e.g. `https://example.com/?ref=user1`
    43. should be turned into links to the individual stores with different
    44. parameters such as: `https://store1.example/?p=1` and
    45. `https://store2.example/?aff=y2` etc.
    46.  
    47. When you want to display a link to one of the products, call the
    48. function `affiliate_link()` with the normal URL to the store and
    49. the name of the store, e.g.
    50.  
    51. `<?= affiliate_link('https://store1.example/', 'store1'); ?>`
    52.  
    53. The function will add the appropriate query string parameters to
    54. the store1 link based on the referral code in the URL if present.
    55.  
    56. Note: If you want to persist this across pageloads, you'll need to
    57. modify the function to store the main affiliate code in the session.
    58.  
    59.  
    60. */
    61.  
    62. $AFFILIATES = [
    63. 'user1' => [
    64. 'store1' => ['p' => '1'],
    65. 'store2' => ['aff' => 'y2'],
    66. 'store3' => ['aff' => '1'],
    67. 'store4' => ['tag' => 'hello-20'],
    68. ],
    69. 'user2' => [
    70. 'store3' => ['aff' => 'y3'],
    71. ],
    72. 'user3' => [
    73. 'store1' => ['p' => '3', 'q' => '4'],
    74. ],
    75. 'user4' => [
    76. 'store2' => ['aff' => 'y8'],
    77. 'store3' => ['aff' => '4'],
    78. ],
    79. ];
    80.  
    81. function affiliate_link($link, $store) {
    82. global $AFFILIATES;
    83.  
    84. // Find current affiliate from query string
    85. if(isset($_GET['ref'])) {
    86. $affiliate = $_GET['ref'];
    87. } else {
    88. // default to the first affiliate defined
    89. $affiliate = $AFFILIATES[];
    90. }
    91.  
    92. // Check if that affiliate has a tracking code for the given store
    93. if(isset($AFFILIATES[$affiliate][$store]))
    94. $params = $AFFILIATES[$affiliate][$store];
    95. else
    96. $params = [];
    97.  
    98. // Add tracking code to the store link
    99. return add_query_params_to_url($link, $params);
    100. }
    101.  
    102.  
    103. function add_query_params_to_url($url, $add_params) {
    104. $parts = parse_url($url);
    105. if(array_key_exists('query', $parts) && $parts['query']) {
    106. parse_str($parts['query'], $params);
    107. } else {
    108. $params = [];
    109. }
    110.  
    111. foreach($add_params as $k=>$v) {
    112. $params[$k] = $v;
    113. }
    114.  
    115. $parts['query'] = http_build_query($params);
    116.  
    117. return build_url($parts);
    118. }
    119.  
    120. // Inverse of parse_url()
    121. // http://php.net/parse_url
    122. function build_url($parsed_url) {
    123. $scheme = !empty($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
    124. $host = !empty($parsed_url['host']) ? $parsed_url['host'] : '';
    125. $port = !empty($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
    126. $user = !empty($parsed_url['user']) ? $parsed_url['user'] : '';
    127. $pass = !empty($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
    128. $pass = ($user || $pass) ? "$pass@" : '';
    129. $path = !empty($parsed_url['path']) ? $parsed_url['path'] : '';
    130. $query = !empty($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
    131. $fragment = !empty($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
    132. return "$scheme$user$pass$host$port$path$query$fragment";
    133. }
    Portland, Oregon • 61°F
    Sat, Feb 12, 2022 1:50pm -08:00
Posted in /code using quill.p3k.io

Hi, I'm Aaron Parecki, Director of Identity Standards at Okta, and co-founder of IndieWebCamp. I maintain oauth.net, write and consult about OAuth, and participate in the OAuth Working Group at the IETF. I also help people learn about video production and livestreaming. (detailed bio)

I've been tracking my location since 2008 and I wrote 100 songs in 100 days. I've spoken at conferences around the world about owning your data, OAuth, quantified self, and explained why R is a vowel. Read more.

  • Director of Identity Standards at Okta
  • IndieWebCamp Founder
  • OAuth WG Editor
  • OpenID Board Member

  • 🎥 YouTube Tutorials and Reviews
  • 🏠 We're building a triplex!
  • ⭐️ Life Stack
  • ⚙️ Home Automation
  • All
  • Articles
  • Bookmarks
  • Notes
  • Photos
  • Replies
  • Reviews
  • Trips
  • Videos
  • Contact
© 1999-2025 by Aaron Parecki. Powered by p3k. This site supports Webmention.
Except where otherwise noted, text content on this site is licensed under a Creative Commons Attribution 3.0 License.
IndieWebCamp Microformats Webmention W3C HTML5 Creative Commons
WeChat ID
aaronpk_tv