Skip to content

Commit 200823d

Browse files
committed
Add new optimized impressions strategy
1 parent 125c93c commit 200823d

4 files changed

Lines changed: 41 additions & 24 deletions

File tree

src/App.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class ui extends Element {
2424
}
2525

2626
if (msg.type == 'init') {
27-
this.insertDisplay(msg.AD_LAST_SHOWN_DATE)
27+
this.insertDisplay(msg.AD_LAST_SHOWN_DATE, msg.AD_LAST_SHOWN_IMPRESSION)
2828
}
2929
})
3030

@@ -38,9 +38,10 @@ class ui extends Element {
3838
Router.on('change:url', url => this.showActiveView(url))
3939
}
4040

41-
insertDisplay(lastShownDate) {
41+
insertDisplay(lastShownDate, lastShownImpression) {
4242
let elem = document.createElement('c-display')
4343
elem.setAttribute('lastshowndate', lastShownDate)
44+
elem.setAttribute('lastshownimpression', lastShownImpression)
4445
elem.setAttribute('hidden', '')
4546
document.body.insertBefore(elem, document.body.querySelector('root-ui'))
4647
}

src/Core.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,14 @@ Promise.all([
149149
figma.clientStorage.getAsync('UUID'),
150150
figma.clientStorage.getAsync('preferences'),
151151
figma.clientStorage.getAsync('AD_LAST_SHOWN_DATE'),
152+
figma.clientStorage.getAsync('AD_LAST_SHOWN_IMPRESSION'),
152153
figma.clientStorage.getAsync('spacing') // legacy
153154
]).then(values => {
154155
let UUID = values[0]
155156
let preferences = values[1]
156157
let AD_LAST_SHOWN_DATE = values[2] || 572083200 // initial date, if no date was saved previously
157-
let spacing = values[3]
158+
let AD_LAST_SHOWN_IMPRESSION = values[3] || 0 // initial impressions
159+
let spacing = values[4]
158160

159161
let SPACING = { x: 100, y: 200 }
160162
let START_NAME = '000'
@@ -224,7 +226,8 @@ Promise.all([
224226
UUID: UUID,
225227
cmd: cmd,
226228
preferences: preferences,
227-
AD_LAST_SHOWN_DATE: AD_LAST_SHOWN_DATE
229+
AD_LAST_SHOWN_DATE: AD_LAST_SHOWN_DATE,
230+
AD_LAST_SHOWN_IMPRESSION: AD_LAST_SHOWN_IMPRESSION
228231
})
229232
figma.ui.postMessage({ type: 'selection', selection: figma.currentPage.selection })
230233

@@ -252,6 +255,11 @@ Promise.all([
252255
if (msg.type === 'displayImpression') {
253256
figma.ui.resize(320, 540+124)
254257
figma.clientStorage.setAsync('AD_LAST_SHOWN_DATE', Date.now())
258+
figma.clientStorage.setAsync('AD_LAST_SHOWN_IMPRESSION', parseInt(AD_LAST_SHOWN_IMPRESSION)+1)
259+
}
260+
261+
if (msg.type === 'resetImpression') {
262+
figma.clientStorage.setAsync('AD_LAST_SHOWN_IMPRESSION', 0)
255263
}
256264
}
257265
}

src/ui/components/display/DisplayComponent.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@ import Element from 'src/ui/Element'
33
import DisplayNetwork from 'src/utils/DisplayNetwork'
44
import Tracking from 'src/utils/Tracking'
55

6-
const PLUGIN_NAME = 'super_tidy'
6+
const PLUGIN_NAME = 'content_buddy'
77

88
class DisplayComponent extends Element {
99

1010
beforeMount() {
1111
// avoid display without proper data
1212
if (this.attrs.lastshowndate == 'undefined') return
1313

14-
DisplayNetwork.getAvailableAd(this.attrs.lastshowndate)
14+
DisplayNetwork.getAvailableAd(this.attrs.lastshowndate, this.attrs.lastshownimpression)
1515
.then(ad => {
1616
// if we have an available ad, then render and display it
1717
if (!!ad) {
18-
ad.link = ad.link + '?utm_source=figma&utm_medium=partner&utm_campaign='+PLUGIN_NAME
18+
ad.link = ad.link + '&utm_campaign='+PLUGIN_NAME
1919
this.data.ad = ad
2020
this.showDisplay()
2121
}

src/utils/DisplayNetwork.js

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class DisplayNetwork {
1717
return fetch(this.root, { method: 'GET' })
1818
}
1919

20-
getAvailableAd(lastShownDate) {
20+
getAvailableAd(lastShownDate, lastShownImpression) {
2121
return new Promise((resolve, reject) => {
2222
this.getAds().then(response => {
2323
response.json().then(ads => {
2424

2525
let availableAds = ads.reduce((prev, current) => {
26-
if (this.checkImpressionAvailability(parseInt(lastShownDate), current)) {
26+
if (this.checkImpressionAvailability(parseInt(lastShownDate), parseInt(lastShownImpression), current)) {
2727
prev.push(current)
2828
}
2929
return prev
@@ -42,28 +42,36 @@ class DisplayNetwork {
4242
}
4343

4444
getAdInterval(ad) {
45-
if (WP_ENV == 'development') return 1000
45+
if (WP_ENV == 'development') return 10000
4646

47-
let interval = 0
48-
switch (ad.impression_interval) {
49-
case 'daily': interval = DAILY_INTERVAL
50-
break;
51-
case 'weekly': interval = WEEKLY_INTERVAL
52-
break;
53-
case 'monthly': interval = MONTHLY_INTERVAL
54-
break;
55-
}
56-
57-
return interval
47+
return parseInt(ad.impression_net_interval)
5848
}
5949

60-
checkImpressionAvailability(lastShownDate, ad) {
50+
checkImpressionAvailability(lastShownDate, lastShownImpression, ad) {
51+
6152
let adInterval = this.getAdInterval(ad)
6253
let availableTimeWindow = (!lastShownDate || lastShownDate + adInterval < this.timeStampNow)
6354

64-
return availableTimeWindow
65-
}
55+
// Check if the ad is optimised for impression rather than time
56+
if (ad.impression_count) {
57+
let availableImpression = (lastShownImpression < parseInt(ad.impression_count))
58+
59+
if (availableTimeWindow && availableImpression || !availableTimeWindow && availableImpression) {
60+
// meanwhile there is impressions left, spend the impression
61+
return true
62+
} else
63+
if (availableTimeWindow && !availableImpression) {
64+
// when impressions limit is reached and time window is valid, reset the impression
65+
parent.postMessage({ pluginMessage: { type: 'resetImpression' } }, '*')
66+
return false
67+
} else {
68+
return false
69+
}
70+
} else {
71+
return availableTimeWindow
72+
}
6673

74+
}
6775
}
6876

6977
export default new DisplayNetwork()

0 commit comments

Comments
 (0)