Ways of Substituting the Page Title into the ‘q’ Variable of a Directlink

The First Way to Substitute the Page Title into the ‘q’ Variable

If the publisher for some reason cannot dynamically substitute values for the directlink in the &q parameter, it should be written with a keyword that gives a general idea of the traffic. For example, on a download site, you can use the following option: &q=download.

The ‘q’ parameter is required to be added to the code link. Without it, conversion on the platform is significantly worse. In place of {QUERY} you need to set the title of the page. It can be written manually for each link, or it will be substituted automatically depending on the site. Through keyword substitution, you can achieve dynamic value transfer in the ‘q’ parameter.

Let’s use JavaScript as an example: we’ll read the values of the tag <title> and the title meta tag title. To substitute keywords in a directlink, place JavaScript before the closing </head> tag:

<script>
(function() {
  function run() {
    var className = '';
    var suffix = className ? '.' + className : '';
    var template = '{QUERY}';
    var aels = document.querySelectorAll('a' + suffix + ', button' + suffix);
    var tel = document.querySelector('title');
    var mel = document.querySelector('meta[name="og:title"]');
    var keyword = (mel && mel.getAttribute('content')) || tel.innerHTML || document.title || '';

    window.__bd_keyword = window.__bd_query = keyword;

    var arr = [].slice.call(aels);

    for (var i = 0; i < arr.length; i++) {
      var ael = arr[i];
      var newHref = (ael.getAttribute('href') || '').replace(template, encodeURIComponent(keyword));
      var newOnclick = (ael.getAttribute('onclick') || '').replace(template, encodeURIComponent(keyword));
      ael.setAttribute('onclick', newOnclick);
      ael.setAttribute('href', newHref);
    }
  }

  if (window.document.readyState === 'interactive' || window.document.readyState === 'complete') {
    return run();
  } else {
    window.document.addEventListener('DOMContentLoaded', run);
  }
})();
</script>

Place your ad code on your site. We have covered this process in detail in previous posts.

<a href="http://site.com/r/?token=1552a1e85b789c55185f97560d5980dfd5c68339&q={QUERY}" target="_blank"><img src="https://i.ibb.co/3BN3yTx/Download-Button-PNG-Photo.png" alt="Download-Button" border="0" width="250"></a>

Important: The directlink must end with the q={QUERY} parameter. The value of the ‘q’ parameter is case sensitive: {QUERY} must be specified in capital letters. If you write parameters as {Query} or {query}, keyword substitution will not work.

Advanced Substitution Methods

  1. Changing Variables for Searching Keywords

If necessary, you can modify the script so that it receives the keyword from another tag. To do this, modify the following script lines:

var tel = document.querySelector('title');
var mel = document.querySelector('meta[name="og:title"]');

If you want to obtain keywords from page descriptions, use the following script:

var tel = document.querySelector('description');
var mel = document.querySelector('meta[name="og:description"]');

2. Adding New Variables for Keyword Search

You can also add additional variables from which you need to obtain keywords. To do this, create a new variable.

As an example, let’s take the H1 heading from the page we are interested in and write it to the my_var variable:

var my_var = document.getElementsByTagName("h1")[0];


If there are no variables on the page, the value for the keyword variable will be an empty string. The modified script looks like this:

<script>
(function() {
  function run() {
    var className = '';
    var suffix = className ? '.' + className : '';
    var template = '{QUERY}';
    var aels = document.querySelectorAll('a' + suffix + ', button' + suffix);
		
		// DO NOT MODIFY THE CODE ABOVE
		//Example of new variables

    var tel = document.querySelector('description');
		var mel = document.querySelector('meta[name="og:description"]');
		var my_var = document.getElementsByTagName("h1")[0];
    var keyword = (mel && mel.getAttribute('content')) || tel.innerHTML || document.title || my_var.innerHTML || '';

		//End of example
		// DO NOT MODIFY THE CODE BELOW

    window.__bd_keyword = window.__bd_query = keyword;

    var arr = [].slice.call(aels);

    for (var i = 0; i < arr.length; i++) {
      var ael = arr[i];
      var newHref = (ael.getAttribute('href') || '').replace(template, encodeURIComponent(keyword));
      var newOnclick = (ael.getAttribute('onclick') || '').replace(template, encodeURIComponent(keyword));
      ael.setAttribute('onclick', newOnclick);
      ael.setAttribute('href', newHref);
    }
  }

  if (window.document.readyState === 'interactive' || window.document.readyState === 'complete') {
    return run();
  } else {
    window.document.addEventListener('DOMContentLoaded', run);
  }
})();
</script>

Important: Do not make changes to other parts of the JavaScript code as this may lead to errors.

The Second Way to Substitute the Page Title into the ‘q’ Variable

By setting up page title substitution, we can achieve dynamically passing the value in the ‘q’ parameter. We will use title because it describes the content of the page in the most complete and compact way.

Important: The current task can be solved by installing the “WordPress Plugin”, which you will find in the DataCash affiliate program.

To substitute the page title into the ‘q’ variable, create a shortcode. But first, make sure you have a child theme on your site. If you don’t know what a child theme is in WordPress, we recommend that you read this article.

Use the “Child Theme Configurator” plugin to create a child theme. After installation and activation, customize the theme.

In WordPress, go to the ‘Tools‘ -> ‘Child Themes‘ tab. Note that in our case, the theme we are using does not have a child theme.

In the new window, check “Create a new Child Theme” and select the theme to use from the drop-down list. Click “Analyze“.

At the end of the page, click on “Create New Child Theme“.

Once the child theme is created, go to ‘Appearance‘ -> ‘Themes‘.

Check that the new theme is activated.

In your file manager, go to the directory where WordPress is located. Find the child theme folder. It’s usually located at wp-content/themes/your-theme-child. Open functions.php file and add code to the end of the file:

function post_title_shortcode(){ return get_the_title(); } add_shortcode('post_title','post_title_shortcode');

Save the file and make sure the changes take effect. If you add the shortcode [post_title] to your post text, when you view your post, the title of the post will be displayed in the place where you inserted the shortcode.

If everything works correctly, you can make changes to the directlink. It will look like this:

http://site.com/r/?token=1552a1e85b789c55185f97560d5980dfd5c68339&q=[post_title]

Let’s go back to the WordPress ‘Settings‘ -> ‘Ad Inserter‘ section.

In “Block 1”, add a shortcode to the directlink that is related to the “Download” button. The button code should look like this:

<a href="http://site.com/r/?token=1552a1e85b789c55185f97560d5980dfd5c68339&q=[post_title]" target="_blank"><img src="https://i.ibb.co/3BN3yTx/Download-Button-PNG-Photo.png" alt="Download-Button" border="0"  width="250"></a>

Click on “Save All Settings”.

Check if the keyword has been correctly inserted into your link. To do this, open any post on the site, right-click on the “Download” button and click “View Code”. In case of successful placement in the sidebar, you will see a link, provided that the title of the article is “Hello World”:

http:/site.com/r/?token=1552a1e85b789c55185f97560d5980dfd5c68339&q=Hello%20world
Scroll to Top