<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Antoine Laborderie on Medium]]></title>
        <description><![CDATA[Stories by Antoine Laborderie on Medium]]></description>
        <link>https://medium.com/@antoine.laborderie?source=rss-7888c557e39e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*P7e6O5Ghv6A9vvTC4a8VRQ.jpeg</url>
            <title>Stories by Antoine Laborderie on Medium</title>
            <link>https://medium.com/@antoine.laborderie?source=rss-7888c557e39e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 21:58:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@antoine.laborderie/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[DoubleClick ads in Angular 2 based project]]></title>
            <link>https://medium.com/lekooa/doubleclick-ads-in-angular-2-based-project-bbd53b425494?source=rss-7888c557e39e------2</link>
            <guid isPermaLink="false">https://medium.com/p/bbd53b425494</guid>
            <category><![CDATA[doubleclick]]></category>
            <category><![CDATA[angular2]]></category>
            <category><![CDATA[google]]></category>
            <category><![CDATA[typescript]]></category>
            <category><![CDATA[ads]]></category>
            <dc:creator><![CDATA[Antoine Laborderie]]></dc:creator>
            <pubDate>Wed, 03 May 2017 14:02:38 GMT</pubDate>
            <atom:updated>2017-12-08T10:15:36.283Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/814/1*Byc5luKu9eO8jCWWuKyVjA.png" /></figure><p>I was assigned the task of displaying ads from local companies in an Angular 2 project during my internship at <a href="https://medium.com/u/1cf6e5d70ed9">Lekooa</a>.</p><p>When I first started working on an Angular 2 Ionic 2 project, I could not figure out how to display ads from <a href="https://www.google.com/dfp">DoubleClick For Publishers (DFP)</a>.</p><p>In plain JavaScript, it’s an easy task. DoubleClick provides developers lines of code to include in your website/web app, and there you go, ads are showing up!</p><p>If you’re not familiar with Angular, I would advise you to read the official documentation for Angular:</p><p><a href="https://angular.io/">Angular</a></p><p>At first, I wanted to find a module that could help me with DFP ads. Over 3k modules are available on <a href="https://www.github.com/definitelytyped/definitelytyped">DefinitelyTyped’s GitHub</a>. I found <a href="https://www.npmjs.com/package/@types/doubleclick-gpt">doubleclick-gpt</a>, a TypeScript type definition that could help me using DFP ads in our Angular 2 project.</p><p>I’m still a newbie and I cannot figure out how to use it in a component, as <em>googletag</em> is declared as global variable.<em> If you know how, please let me know in the comment section.</em></p><p>I dropped this Type definition and created my own <a href="https://angular.io/docs/ts/latest/api/core/index/Component-decorator.html">Component</a>. I wanted to create a component so I could display an ad with one line of code on any page of our web app.</p><p><a href="https://www.github.com/alaborderie/ng2-ad-dfp">alaborderie/ng2-ad-dfp</a></p><p>As the component was getting ready, I faced another problem. DoubleClick needs you to define your ‘settings’ a while before you can display them. It means that you cannot define your settings in the component. That makes things a little more complicated as you need to define your settings in an external JavaScript file called in your <em>index.html</em>.</p><p>It should be something like this:</p><p>&lt;script src=&#39;assets/scripts/defineAds.js&#39;&gt;&lt;/script&gt;</p><p><em>Basically, this file contains all the code that DoubleClick provides you when you choose to “generate tags” on an ad-unit.</em></p><p>As DoubleClick provides an online library for JavaScript, you should add this line of code to your <em>index.html</em> in your header:</p><p>&lt;script async=&#39;async&#39; src=&#39;https://www.googletagservices.com/tag/js/jpt.js&#39;&gt;&lt;/script&gt;</p><h3>Displaying an ad</h3><p>Now that everything is defined, including a component such as this one is as easy as putting a single line of code in whatever page you want.</p><p>For example, if I want a banner in my login page, I need to edit src/pages/login/login.html and add a single line of code at the very bottom of the file (before &lt;/body&gt;)</p><p>&lt;ad-dfp [type]=&quot;&#39;banner&#39;&quot;&gt;&lt;/ad-dfp&gt;</p><p>You may ask, what is [type] ? It lets the component know that the variable “type” will contain the following variable’s value. If you want to input a string, you need to add single quotes, like “variable” -&gt; “ ‘string’ ”</p><p>The component can get 3 different strings, “banner”, “inter”, and “hidden_inter”. Inter means interstitial and the hidden version does not show up as soon as the page has finished loading, but when an event has been triggered (see below).</p><p>Now the banner should be displayed where the component has been included.<br><em>Note that you need to tell the component which googletag to use. To do so, modify the ad-dfp.js file in the “getTag()” function.</em></p><h3>Detecting AdBlockers</h3><p>As a new TypeScript developer, I wanted to try new things, and decided to add more features to my component. AdBlockers can be a threat for your wallet and I wanted my component to feature an <em>AdBlocker detector.</em></p><p>A question was asked on <a href="http://stackoverflow.com/questions/18180598/ad-blocker-detection-aka-adblock-plus">stackoverflow</a> and an easy to set-up solution was proposed. It consists in declaring a variable in a JS file named “ads.js”<em> <br></em>which will be blocked by most AdBlockers, hence the variable will not be defined.</p><blockquote><em>— tested on AdBlock, AdBlock Pro, uBlock, uBlock Origin —</em></blockquote><p>Then the component checks if the variable has a value or is undefined. If it is undefined, you can choose to display a button with a message asking the user to add the website to his whitelist. He can do it or not.</p><h3>Another feature: Hidden interstitials</h3><p>After all this time spent coding this component, I decided that I should do something better for interstitials. As it was, they would show up just after your browser has finished loading the page. For this project, that was not optimal since we wanted to display the ad after a button was clicked.</p><p>To do so, I defined another type of ad, “hidden_inter”!</p><p>It is just like an interstitial but it does not display through the component. The component “subscribes” to an <a href="https://angular.io/docs/ts/latest/api/platform-browser/index/EventManager-class.html">event</a>, which when triggered, will call the display function. You can then trigger the event wherever you want in your code!</p><h3>Tests, problems and solutions</h3><p>When the component is finally ready, it’s testing time!</p><p>As I was trying to make it bug in any way I figured out that instances of the component were not “dying” properly. If you leave a page and come back to it, it will show 2 interstitials instead of one.</p><p>A google DoubleClick ad is displayed thanks to an iFrame. Those iFrame stay “alive” even after you destroy the div in which you want to display the ad.</p><p>To remove them you have to remove a div created by Google when the call to the function googletag.display() is launched. This can be done through jQuery everytime you leave the page (e.g. in ngOnDestroy() for angular 2). Then, ads will render properly even if you leave and comeback to a page containing ads.</p><p>Creating this component during my internship at <a href="https://medium.com/u/1cf6e5d70ed9">Lekooa</a> helped me a lot in my learning of TypeScript and Angular 2 and I hope it will help other developers to display DoubleClick ads in their web apps!</p><blockquote>If you need more help using my component, feel free to contact me at antoine.laborderie@gmail.com</blockquote><figure><a href="https://www.e-marger.com/"><img alt="" src="https://cdn-images-1.medium.com/max/300/1*XELi5XU8Xe7Lc93GSkFsZw.png" /></a></figure><figure><a href="http://www.lekooa.com/"><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*gYNKizINJWoXqRA0CMmW-g.png" /></a></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bbd53b425494" width="1" height="1" alt=""><hr><p><a href="https://medium.com/lekooa/doubleclick-ads-in-angular-2-based-project-bbd53b425494">DoubleClick ads in Angular 2 based project</a> was originally published in <a href="https://medium.com/lekooa">Lekooa</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>