Jump to content

How to configure Watched Right Now block on Index to render the current user country and flag


Tech Support

Recommended Posts

KVS doesn't support video popularity stats per country yet, but it is possible to emulate this for the end users, providing some personal attitude to every visitor:

country_flag.thumb.png.a0e8dfc84159bcbcb94424f46e2c36d1.png

 

Step 1. In /admin/include/pre_initialize_page_code.php add the following line:

$_GET['GEOIP_COUNTRY_CODE'] = $_REQUEST['GEOIP_COUNTRY_CODE'] = strtolower($_SERVER['GEOIP_COUNTRY_CODE']);

This is needed to pass environmental variable with country code to request variables, that can be used by KVS site engine.

 

Step 2. In Website UI -> Texts add the following text that will be rendered as a header:

  • External ID: videos.list_title_by_country
  • Default: Popular in %1% <img class="flag" title="%1%" alt="%2%" src="/static/images/flags/%2%.gif">

This text also contains IMG with reference to flag image.

 

Step 3. Open Website UI -> Pages -> Index -> Videos Watched Right Now for editing.

Change its Template code to the following:

{{query_kvs select="single" table="list_countries" where_country_code=$smarty.get.GEOIP_COUNTRY_CODE where_language_code="en" assign="country"}}
{{assign var="country_title" value=$country.title}}
{{if !$country_title || $country.is_system==1}}
	{{assign var="list_videos_title" value=$lang.videos.list_title_watched_right_now}}
{{else}}
	{{assign var="list_videos_title" value=$lang.videos.list_title_by_country|replace:"%1%":$country.title|replace:"%2%":$smarty.get.GEOIP_COUNTRY_CODE|smarty:nodefaults}}
{{/if}}
{{assign var="list_videos_use_h1" value="true"}}
{{include file="include_list_videos_block_common.tpl"}}

NOTE: KVS only supports country names in English and Russian languages, if your site is in Russian please change where_language_code="en" with "ru" to have country name in Russian.

In block settings down the page specify GEOIP_COUNTRY_CODE under Dynamic HTTP parameters. This is needed for correct caching logic for this block. If you don't do that, KVS admin panel will show error that using $smarty.get.GEOIP_COUNTRY_CODE variable will not work correctly.

The code does the following. First it queries KVS database and selects country record that matches the current visitor's country code using query_kvs template function. Then it checks whether any non-system country record is found (system countries are some satellite ISPs and anonymous proxies). If nothing is found, this code will show old "Videos Watched Right Now" text. Otherwise it will show the new text with country code and flag icon.

 

Step 4. For better icon styling add this block into theme CSS file:

h1 img.flag,
h2 img.flag {
  vertical-align: middle;
}

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
On 11/16/2020 at 8:51 AM, Tech Support said:

Step 4. For better icon styling add this block into theme CSS file:


h1 img.flag {
  vertical-align: middle;
}

Thanks!

referred to in step 4, in all-responsive-metal.css ?

I have added the text at the end: h1 img.flag { vertical-align: middle; } but the flag icon is not centered.

Link to comment
Share on other sites

9 hours ago, Tech Support said:

We updated this code in post, could be that in your case it is using H2 to render the header. Try that.

Another possible issue is that CSS file is cached in your browser and your changes are not loaded. Try to check in another browser.

Solved, thanks.

Link to comment
Share on other sites

  • 3 months later...
5 hours ago, Emilia said:

To fix the above code, all you need to do is replace GEOIP_COUNTRY_CODE to MM_COUNTRY_CODE everywhere. Everything else remains the same

No, no, if another module uses another ENV variable to store country code (e.g. MM_COUNTRY_CODE instead of GEOIP_COUNTRY_CODE needed by KVS), then better add this line into /admin/include/setup.php to move country code to the needed variable:

$_SERVER['GEOIP_COUNTRY_CODE'] = $_SERVER['MM_COUNTRY_CODE'];

This will also ensure that all other KVS functionality that relies on GEOIP will function correctly.

  • Like 1
Link to comment
Share on other sites

Hi, I noticed I have this in my setup.php

 

if (isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
	$_SERVER['ORIG_REMOTE_ADDR']=$_SERVER['REMOTE_ADDR'];
	$_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_FORWARDED_FOR'];
	if (strpos($_SERVER['REMOTE_ADDR'],',')!==false)
	{
		$_SERVER['REMOTE_ADDR']=trim(substr($_SERVER['REMOTE_ADDR'],0,strpos($_SERVER['REMOTE_ADDR'],',')));
	}
} elseif (isset($_SERVER['HTTP_X_REAL_IP']))
{
	$_SERVER['ORIG_REMOTE_ADDR']=$_SERVER['REMOTE_ADDR'];
	$_SERVER['REMOTE_ADDR']=$_SERVER['HTTP_X_REAL_IP'];
}
if (isset($_SERVER['HTTP_CF_IPCOUNTRY']))
{
	$_SERVER['GEOIP_COUNTRY_CODE']=$_SERVER['HTTP_CF_IPCOUNTRY'];
}
if (function_exists('geoip_country_code_by_name'))
{
    $_SERVER['GEOIP_COUNTRY_CODE'] = geoip_country_code_by_name($_SERVER['REMOTE_ADDR']);
}

Where should I add this?

$_SERVER['GEOIP_COUNTRY_CODE'] = $_SERVER['MM_COUNTRY_CODE'];

I dont use cloudflare so i probably don't need any HTTP_CF_IPCOUNTRY header

Edited by Emilia
Link to comment
Share on other sites

15 hours ago, Tech Support said:

Just after all this block. This block is designed in the same way to detect country from Cloudflare headers.

Hello, so for correct operation you have to replace this code:
    $_SERVER['GEOIP_COUNTRY_CODE']=$_SERVER['HTTP_CF_IPCOUNTRY'];

for this?

$_SERVER['GEOIP_COUNTRY_CODE'] = $_SERVER['MM_COUNTRY_CODE'];
Link to comment
Share on other sites

6 hours ago, davex said:

Hello, so for correct operation you have to replace this code:
    $_SERVER['GEOIP_COUNTRY_CODE']=$_SERVER['HTTP_CF_IPCOUNTRY'];

for this?


$_SERVER['GEOIP_COUNTRY_CODE'] = $_SERVER['MM_COUNTRY_CODE'];

No need to replace anything, this code is not for all users, this code is for specific question asked above. If you also have a case, when country code is put into MM_COUNTRY_CODE environment variable, then you also need to add this code. If not, then nothing should be changed / replaced / added.

  • Thanks 1
Link to comment
Share on other sites

  • 2 years later...

I just tried and followed the all steps but for me nothing changed on the homepage. Its still the same as "Videos Being Watched" . It can be cache but i tried incognito mode and other browsers but its still the same. Am i doing anything wrong ? i followed every step in the tutorial .

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...