Home > Technical > Using and Extending Variables in Your Links

Using and Extending Variables in Your Links

Table of Contents
< All Topics

Using and Extending Variables in Your Links

Introduction

In affiliate marketing, optimizing your links for better performance is crucial. LinkGenius, a powerful WordPress plugin, allows you to not only manage and track your affiliate links efficiently but also provides a robust variable system to enhance your link customization. In this tutorial, we’ll explore how to use and extend variables in LinkGenius.

Understanding Variables in LinkGenius

What are Variables?

In the context of LinkGenius, variables are placeholders that can be used to dynamically insert values into your target URLs or Google Analytics 4 (GA4) events or parameters. These placeholders are replaced with actual values when the link is clicked or an event is triggered.

Why Use Variables?

Variables add a layer of flexibility and customization to your affiliate links. They allow you to tailor the target URLs or event name and parameters based on dynamic information, providing a personalized experience for your audience.

Using Native Variables

LinkGenius comes with a set of native variables that you can directly incorporate into your links:

  • {referrer}: Referring URL of the click.
  • {client_id}: Unique client identifier.
  • {categories}: Categories associated with the link.
  • {tags}: Tags linked to the URL.
  • {target_url}: The target URL provide when creating the link.
  • {link_slug}: Slug of the link.
  • {link_id}: ID of the link.
  • {COOKIE[(.*?)]}: Retrieve data from a cookie. Replace (.*?) with the cookie name.
  • {GET[(.*?)]}: Fetch data from the URL query parameters. Replace (.*?) with the query parameter name.
  • {SESSION[(.*?)]}: Access data from the session. Replace (.*?) with the session variable name.

In order for LinkGenius to recognize the variables, you need to place the names within curly brackets {} in the target URLs or GA4 event configurations.

Extending Variables with LinkGenius Hooks

LinkGenius allow you to extend or replace the native variables by providing a convenient filter hook.

Filter: linkgenius_variables

To extend or replace native variables, LinkGenius provides a WordPress Hook named linkgenius_variables. This filter takes an array of variables and expects you to return the adjusted array. Each item in the array consists of the variable name as the key and a function that accepts the $link_id, $on_redirect parameter, and as third parameter the optional additional name captured by (.*?) in the variable name.

Example: Extending Variables

Assuming basic PHP knowledge, let’s consider extending the variables with the {country} variable. Add the following code to your theme’s functions.php file:

function custom_linkgenius_variables($variables) {
    // Adding a new variable: {country}
    $variables['{country}'] = function($link_id) {
        $country = Redirect_PRO::get_user_country();
        return $country;
    };

    // You can also modify existing variables here

    return $variables;
}

add_filter('linkgenius_variables', 'custom_linkgenius_variables');

In this example, we’ve added a new variable {country} and provided a function to dynamically fetch users country.

Conclusion

By understanding and leveraging variables in LinkGenius, you can take your affiliate marketing strategy to new heights. Whether you’re a beginner or have some PHP knowledge, this guide equips you with the knowledge to use native variables and extend them to suit your specific needs. Experiment with these tools to enhance the personalization and effectiveness of your affiliate links. Happy optimizing!

Tags: