Disabling AdSense based on cookies

Hello there,

So I have quite a tough question about AdSense.

I'm in a partnership with one advertising company and I need, when visitors come throught this company, I need to show their ads for the whole time. When they leave, they still should after 2 minutes see this companies ads, but after 2min 1sec no more. And I have difficulty hiding AdSense - it still show (not in the sidebar but) in footer and I don't know why...

So I have one situation - Normal where AdSense shows (without referral)

This is where all AdSense positions are showing for example this one:

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- Trendy Bydlení - Sidebar  -->
<ins class="adsbygoogle"
if ( ads == 1 ) {
(adsbygoogle = window.adsbygoogle || []).push({});

And the second situation - Referral where my ads partner shows (with referral)

For this, I have in header.php

<script src="//i.imedia.cz/js/im3light.js"></script>
<script> var pozice = [{id:"leaderboard", zoneId: "external.test.leaderboard"},{id:"skystraper", zoneId: "external.test.skyscraper"}, {id:"square", zoneId: "external.test.smr"}];</script>
im.conf.server = "iimedia.sbeta.cz"; 

function getQueryVariable(variable)
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++)
    var pair = vars[i].split("=");
    if (pair[0] == variable)
      return pair[1];
  return -1; //not found

if ( getQueryVariable('utm_source') == 'www.seznam.cz' )
   var ads = 1;
} else
    var ads = 0;

And than I show the ad simply like this:
<div id="skystraper"></div>

When accessing site without referral, everything is fine but with the second situation AdSense are still showing and weirdly displaying in footer. Was anybody solving similar problem?

First situation - fine

Second situation - wrong