Skip to content

Commit 9b193a2

Browse files
authored
feat(ads): Add slot id prop to dfp ad slots (#116)
1 parent 416757b commit 9b193a2

File tree

1 file changed

+50
-23
lines changed

1 file changed

+50
-23
lines changed

src/components/dfp-ad.js

Lines changed: 50 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import React from 'react';
2-
import {AdSlot, DFPSlotsProvider, DFPManager} from 'react-dfp';
3-
import {connect} from 'react-redux';
4-
import {withError} from './with-error';
5-
1+
import React from "react";
2+
import { AdSlot, DFPManager, DFPSlotsProvider } from "react-dfp";
3+
import { connect } from "react-redux";
4+
import { withError } from "./with-error";
65

76
/**
87
* This is a function which can be used to manage ad units in a single place. A component must be created, and used with the `adtype` parameter. These ads are lazy-loaded and single-request mode is disabled by default which can be overwritten as follows.
@@ -36,34 +35,62 @@ import {withError} from './with-error';
3635
* @param {Object} params
3736
* @param {string} params.defaultNetworkID - Network Id of the Ad Provider
3837
* @param {Object} params.config - Configuration of the ads (see example)
38+
* @param {string} params.slotId - A unique string identify the Ad slot
3939
* @param {function} params.targeting - Function which takes in the current state, props from the parent component and returns targetting parameters
4040
* @param {boolean} params.collapseEmptyDivs (default true)
4141
* @param {boolean} params.lazyLoad (default true)
4242
* @param {boolean} params.singleRequest - Run Dfp in Single Request Mode (default false)
4343
* @category Ads
4444
* @returns {Component} A component that can
4545
*/
46-
export function createDfpAdComponent({ defaultNetworkID, config, targeting, collapseEmptyDivs = true, lazyLoad = true, singleRequest = false }) {
47-
return connect((state, ownProps) => ({
48-
targetingArguments: targeting(state, ownProps),
49-
defaultNetworkID: defaultNetworkID,
50-
config: config,
51-
collapseEmptyDivs: collapseEmptyDivs,
52-
lazyLoad: lazyLoad,
53-
singleRequest: singleRequest
54-
}), () => ({}))(withError(DfpAdBase));
46+
export function createDfpAdComponent({
47+
defaultNetworkID,
48+
config,
49+
slotId,
50+
targeting,
51+
collapseEmptyDivs = true,
52+
lazyLoad = true,
53+
singleRequest = false
54+
}) {
55+
return connect(
56+
(state, ownProps) => ({
57+
targetingArguments: targeting(state, ownProps),
58+
defaultNetworkID: defaultNetworkID,
59+
config: config,
60+
slotId: slotId,
61+
collapseEmptyDivs: collapseEmptyDivs,
62+
lazyLoad: lazyLoad,
63+
singleRequest: singleRequest
64+
}),
65+
() => ({})
66+
)(withError(DfpAdBase));
5567
}
5668

57-
function DfpAdBase({defaultNetworkID, config, collapseEmptyDivs, targetingArguments, adtype, lazyLoad, singleRequest}) {
69+
function DfpAdBase({
70+
defaultNetworkID,
71+
config,
72+
slotId,
73+
collapseEmptyDivs,
74+
targetingArguments,
75+
adtype,
76+
lazyLoad,
77+
singleRequest
78+
}) {
5879
const adConfig = config[adtype];
59-
return <DFPSlotsProvider dfpNetworkId={defaultNetworkID}
60-
collapseEmptyDivs={collapseEmptyDivs}
61-
targetingArguments={targetingArguments}
62-
sizeMapping={adConfig.viewPortSizeMapping}
63-
lazyLoad={lazyLoad}
64-
singleRequest={singleRequest}>
65-
<AdSlot {...adConfig} />
66-
</DFPSlotsProvider>;
80+
81+
const adProps = { slotId, ...adConfig };
82+
return (
83+
<DFPSlotsProvider
84+
dfpNetworkId={defaultNetworkID}
85+
collapseEmptyDivs={collapseEmptyDivs}
86+
targetingArguments={targetingArguments}
87+
sizeMapping={adConfig.viewPortSizeMapping}
88+
lazyLoad={lazyLoad}
89+
singleRequest={singleRequest}
90+
>
91+
<AdSlot {...adProps} />
92+
</DFPSlotsProvider>
93+
);
6794
}
6895

6996
export function refreshDfpAds(adSlots) {

0 commit comments

Comments
 (0)