title: How to use Google DFA (Google Publisher Tag) in SPA?
body: <p>I am using Google DFA (gpt.js) in Angular.</p>
  
  <p>The first time an ad is displaying fine but second time it gives null error.</p>
  
  <p>I have directive which has the following method, called in <code>OnInit</code> method:</p>
  
  <pre><code>addGoogleAd() {
    const googletag: any = window['googletag'] || {};
  
    if (window['googletag'] &amp;&amp; googletag.apiReady) {
      googletag.cmd = googletag.cmd || [];
  
      googletag.cmd.push(() =&gt; {
        googletag.pubads().setTargeting('site', 'weatherinnovations');
        googletag.pubads().enableSingleRequest();
  
        googletag.defineSlot('/xx/weather_innovations', [[160, 600]], 'div-gpt-ad-xx-1')
          .setTargeting('position', ['s1']).addService(googletag.pubads());
        googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-2')
          .setTargeting('position', ['s2']).addService(googletag.pubads());
        googletag.defineSlot('/xx/weather_innovations', [[300, 250]], 'div-gpt-ad-xx-3')
          .setTargeting('position', ['s3']).addService(googletag.pubads());
        googletag.defineSlot('/76643716/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-4')
          .setTargeting('position', ['s4']).addService(googletag.pubads());
        googletag.defineSlot('/xx/weather_innovations', [[728, 90]], 'div-gpt-ad-xx-5')
          .setTargeting('position', ['s5']).addService(googletag.pubads());
  
        googletag.pubads().collapseEmptyDivs(true, true);
        googletag.enableServices();
      });
      googletag.display(this.id);
    }
  }
  </code></pre>
  
  <p>The dashboard loads and displays ad, but when I return back from another component it gives the following error:</p>
  
  <blockquote>
    <p>pubads_impl_265.js:1 Exception in queued GPT command TypeError: Cannot
    read property 'setTargeting' of null</p>
  </blockquote>
  
  <p>I consoled the <code>googletag</code> and is not empty, and has same content.</p>