embulk-input-facebook_ads_insights 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (27) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +13 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +78 -0
  5. data/build.gradle +99 -0
  6. data/config/checkstyle/checkstyle.xml +128 -0
  7. data/config/checkstyle/default.xml +108 -0
  8. data/gradle/wrapper/gradle-wrapper.jar +0 -0
  9. data/gradle/wrapper/gradle-wrapper.properties +5 -0
  10. data/gradlew +172 -0
  11. data/gradlew.bat +84 -0
  12. data/lib/embulk/input/facebook_ads_insights.rb +3 -0
  13. data/src/main/java/org/embulk/input/facebook_ads_insights/AdsInsightsAccessor.java +149 -0
  14. data/src/main/java/org/embulk/input/facebook_ads_insights/Client.java +243 -0
  15. data/src/main/java/org/embulk/input/facebook_ads_insights/FacebookAdsInsightsColumnVisitor.java +123 -0
  16. data/src/main/java/org/embulk/input/facebook_ads_insights/FacebookAdsInsightsInputPlugin.java +79 -0
  17. data/src/main/java/org/embulk/input/facebook_ads_insights/PluginTask.java +80 -0
  18. data/src/main/java/org/embulk/input/facebook_ads_insights/Util.java +18 -0
  19. data/src/main/java/org/embulk/input/facebook_ads_insights/model/ActionAttributionWindow.java +43 -0
  20. data/src/main/java/org/embulk/input/facebook_ads_insights/model/ActionBreakdown.java +47 -0
  21. data/src/main/java/org/embulk/input/facebook_ads_insights/model/ActionReportTime.java +38 -0
  22. data/src/main/java/org/embulk/input/facebook_ads_insights/model/Breakdown.java +69 -0
  23. data/src/main/java/org/embulk/input/facebook_ads_insights/model/DatePreset.java +55 -0
  24. data/src/main/java/org/embulk/input/facebook_ads_insights/model/Level.java +40 -0
  25. data/src/main/java/org/embulk/input/facebook_ads_insights/model/ObjectType.java +30 -0
  26. data/src/test/java/org/embulk/input/facebook_ads_insights/TestFacebookAdsInsightsInputPlugin.java +5 -0
  27. metadata +106 -0
Binary file
@@ -0,0 +1,5 @@
1
+ distributionBase=GRADLE_USER_HOME
2
+ distributionPath=wrapper/dists
3
+ zipStoreBase=GRADLE_USER_HOME
4
+ zipStorePath=wrapper/dists
5
+ distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-bin.zip
data/gradlew ADDED
@@ -0,0 +1,172 @@
1
+ #!/usr/bin/env sh
2
+
3
+ ##############################################################################
4
+ ##
5
+ ## Gradle start up script for UN*X
6
+ ##
7
+ ##############################################################################
8
+
9
+ # Attempt to set APP_HOME
10
+ # Resolve links: $0 may be a link
11
+ PRG="$0"
12
+ # Need this for relative symlinks.
13
+ while [ -h "$PRG" ] ; do
14
+ ls=`ls -ld "$PRG"`
15
+ link=`expr "$ls" : '.*-> \(.*\)$'`
16
+ if expr "$link" : '/.*' > /dev/null; then
17
+ PRG="$link"
18
+ else
19
+ PRG=`dirname "$PRG"`"/$link"
20
+ fi
21
+ done
22
+ SAVED="`pwd`"
23
+ cd "`dirname \"$PRG\"`/" >/dev/null
24
+ APP_HOME="`pwd -P`"
25
+ cd "$SAVED" >/dev/null
26
+
27
+ APP_NAME="Gradle"
28
+ APP_BASE_NAME=`basename "$0"`
29
+
30
+ # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31
+ DEFAULT_JVM_OPTS=""
32
+
33
+ # Use the maximum available, or set MAX_FD != -1 to use that value.
34
+ MAX_FD="maximum"
35
+
36
+ warn () {
37
+ echo "$*"
38
+ }
39
+
40
+ die () {
41
+ echo
42
+ echo "$*"
43
+ echo
44
+ exit 1
45
+ }
46
+
47
+ # OS specific support (must be 'true' or 'false').
48
+ cygwin=false
49
+ msys=false
50
+ darwin=false
51
+ nonstop=false
52
+ case "`uname`" in
53
+ CYGWIN* )
54
+ cygwin=true
55
+ ;;
56
+ Darwin* )
57
+ darwin=true
58
+ ;;
59
+ MINGW* )
60
+ msys=true
61
+ ;;
62
+ NONSTOP* )
63
+ nonstop=true
64
+ ;;
65
+ esac
66
+
67
+ CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68
+
69
+ # Determine the Java command to use to start the JVM.
70
+ if [ -n "$JAVA_HOME" ] ; then
71
+ if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72
+ # IBM's JDK on AIX uses strange locations for the executables
73
+ JAVACMD="$JAVA_HOME/jre/sh/java"
74
+ else
75
+ JAVACMD="$JAVA_HOME/bin/java"
76
+ fi
77
+ if [ ! -x "$JAVACMD" ] ; then
78
+ die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79
+
80
+ Please set the JAVA_HOME variable in your environment to match the
81
+ location of your Java installation."
82
+ fi
83
+ else
84
+ JAVACMD="java"
85
+ which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86
+
87
+ Please set the JAVA_HOME variable in your environment to match the
88
+ location of your Java installation."
89
+ fi
90
+
91
+ # Increase the maximum file descriptors if we can.
92
+ if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93
+ MAX_FD_LIMIT=`ulimit -H -n`
94
+ if [ $? -eq 0 ] ; then
95
+ if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96
+ MAX_FD="$MAX_FD_LIMIT"
97
+ fi
98
+ ulimit -n $MAX_FD
99
+ if [ $? -ne 0 ] ; then
100
+ warn "Could not set maximum file descriptor limit: $MAX_FD"
101
+ fi
102
+ else
103
+ warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104
+ fi
105
+ fi
106
+
107
+ # For Darwin, add options to specify how the application appears in the dock
108
+ if $darwin; then
109
+ GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110
+ fi
111
+
112
+ # For Cygwin, switch paths to Windows format before running java
113
+ if $cygwin ; then
114
+ APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115
+ CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116
+ JAVACMD=`cygpath --unix "$JAVACMD"`
117
+
118
+ # We build the pattern for arguments to be converted via cygpath
119
+ ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120
+ SEP=""
121
+ for dir in $ROOTDIRSRAW ; do
122
+ ROOTDIRS="$ROOTDIRS$SEP$dir"
123
+ SEP="|"
124
+ done
125
+ OURCYGPATTERN="(^($ROOTDIRS))"
126
+ # Add a user-defined pattern to the cygpath arguments
127
+ if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128
+ OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129
+ fi
130
+ # Now convert the arguments - kludge to limit ourselves to /bin/sh
131
+ i=0
132
+ for arg in "$@" ; do
133
+ CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134
+ CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135
+
136
+ if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137
+ eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138
+ else
139
+ eval `echo args$i`="\"$arg\""
140
+ fi
141
+ i=$((i+1))
142
+ done
143
+ case $i in
144
+ (0) set -- ;;
145
+ (1) set -- "$args0" ;;
146
+ (2) set -- "$args0" "$args1" ;;
147
+ (3) set -- "$args0" "$args1" "$args2" ;;
148
+ (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149
+ (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150
+ (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151
+ (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152
+ (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153
+ (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154
+ esac
155
+ fi
156
+
157
+ # Escape application args
158
+ save () {
159
+ for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160
+ echo " "
161
+ }
162
+ APP_ARGS=$(save "$@")
163
+
164
+ # Collect all arguments for the java command, following the shell quoting and substitution rules
165
+ eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166
+
167
+ # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168
+ if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169
+ cd "$(dirname "$0")"
170
+ fi
171
+
172
+ exec "$JAVACMD" "$@"
data/gradlew.bat ADDED
@@ -0,0 +1,84 @@
1
+ @if "%DEBUG%" == "" @echo off
2
+ @rem ##########################################################################
3
+ @rem
4
+ @rem Gradle startup script for Windows
5
+ @rem
6
+ @rem ##########################################################################
7
+
8
+ @rem Set local scope for the variables with windows NT shell
9
+ if "%OS%"=="Windows_NT" setlocal
10
+
11
+ set DIRNAME=%~dp0
12
+ if "%DIRNAME%" == "" set DIRNAME=.
13
+ set APP_BASE_NAME=%~n0
14
+ set APP_HOME=%DIRNAME%
15
+
16
+ @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17
+ set DEFAULT_JVM_OPTS=
18
+
19
+ @rem Find java.exe
20
+ if defined JAVA_HOME goto findJavaFromJavaHome
21
+
22
+ set JAVA_EXE=java.exe
23
+ %JAVA_EXE% -version >NUL 2>&1
24
+ if "%ERRORLEVEL%" == "0" goto init
25
+
26
+ echo.
27
+ echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28
+ echo.
29
+ echo Please set the JAVA_HOME variable in your environment to match the
30
+ echo location of your Java installation.
31
+
32
+ goto fail
33
+
34
+ :findJavaFromJavaHome
35
+ set JAVA_HOME=%JAVA_HOME:"=%
36
+ set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37
+
38
+ if exist "%JAVA_EXE%" goto init
39
+
40
+ echo.
41
+ echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42
+ echo.
43
+ echo Please set the JAVA_HOME variable in your environment to match the
44
+ echo location of your Java installation.
45
+
46
+ goto fail
47
+
48
+ :init
49
+ @rem Get command-line arguments, handling Windows variants
50
+
51
+ if not "%OS%" == "Windows_NT" goto win9xME_args
52
+
53
+ :win9xME_args
54
+ @rem Slurp the command line arguments.
55
+ set CMD_LINE_ARGS=
56
+ set _SKIP=2
57
+
58
+ :win9xME_args_slurp
59
+ if "x%~1" == "x" goto execute
60
+
61
+ set CMD_LINE_ARGS=%*
62
+
63
+ :execute
64
+ @rem Setup the command line
65
+
66
+ set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67
+
68
+ @rem Execute Gradle
69
+ "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70
+
71
+ :end
72
+ @rem End local scope for the variables with windows NT shell
73
+ if "%ERRORLEVEL%"=="0" goto mainEnd
74
+
75
+ :fail
76
+ rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77
+ rem the _cmd.exe /c_ return code!
78
+ if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79
+ exit /b 1
80
+
81
+ :mainEnd
82
+ if "%OS%"=="Windows_NT" endlocal
83
+
84
+ :omega
@@ -0,0 +1,3 @@
1
+ Embulk::JavaPlugin.register_input(
2
+ "facebook_ads_insights", "org.embulk.input.facebook_ads_insights.FacebookAdsInsightsInputPlugin",
3
+ File.expand_path('../../../../classpath', __FILE__))
@@ -0,0 +1,149 @@
1
+ package org.embulk.input.facebook_ads_insights;
2
+
3
+ import com.facebook.ads.sdk.AdsActionStats;
4
+ import com.facebook.ads.sdk.AdsInsights;
5
+ import com.google.gson.JsonElement;
6
+ import org.slf4j.Logger;
7
+ import org.slf4j.LoggerFactory;
8
+
9
+ import java.util.List;
10
+ import java.util.Objects;
11
+ import java.util.stream.Collectors;
12
+
13
+ public class AdsInsightsAccessor
14
+ {
15
+ private final Logger logger = LoggerFactory.getLogger(AdsInsightsAccessor.class);
16
+
17
+ private final AdsInsights adsInsights;
18
+
19
+ public AdsInsightsAccessor(final AdsInsights adsInsights)
20
+ {
21
+ this.adsInsights = adsInsights;
22
+ }
23
+
24
+ public String get(String name)
25
+ {
26
+ switch (name) {
27
+ case "account_currency": return adsInsights.getFieldAccountCurrency();
28
+ case "account_id": return adsInsights.getFieldAccountId();
29
+ case "account_name": return adsInsights.getFieldAccountName();
30
+ case "action_values": return actionsToString(adsInsights.getFieldActionValues());
31
+ case "actions": return actionsToString(adsInsights.getFieldActions());
32
+ case "ad_id": return adsInsights.getFieldAdId();
33
+ case "ad_name": return adsInsights.getFieldAdName();
34
+ case "adset_id": return adsInsights.getFieldAdsetId();
35
+ case "adset_name": return adsInsights.getFieldAdsetName();
36
+ case "app_store_clicks": return adsInsights.getFieldAppStoreClicks();
37
+ case "buying_type": return adsInsights.getFieldBuyingType();
38
+ case "campaign_id": return adsInsights.getFieldCampaignId();
39
+ case "campaign_name": return adsInsights.getFieldCampaignName();
40
+ case "canvas_avg_view_percent": return adsInsights.getFieldCanvasAvgViewPercent();
41
+ case "canvas_avg_view_time": return adsInsights.getFieldCanvasAvgViewTime();
42
+ case "clicks": return adsInsights.getFieldClicks();
43
+ case "conversion_rate_ranking": return adsInsights.getFieldConversionRateRanking();
44
+ case "conversion_values": return actionsToString(adsInsights.getFieldConversionValues());
45
+ case "conversions": return actionsToString(adsInsights.getFieldConversions());
46
+ case "cost_per_10_sec_video_view": return actionsToString(adsInsights.getFieldCostPer10SecVideoView());
47
+ case "cost_per_action_type": return actionsToString(adsInsights.getFieldCostPerActionType());
48
+ case "cost_per_conversion": return actionsToString(adsInsights.getFieldCostPerConversion());
49
+ case "cost_per_estimated_ad_recallers": return adsInsights.getFieldCostPerEstimatedAdRecallers();
50
+ case "cost_per_inline_link_click": return adsInsights.getFieldCostPerInlineLinkClick();
51
+ case "cost_per_inline_post_engagement": return adsInsights.getFieldCostPerInlinePostEngagement();
52
+ case "cost_per_outbound_click": return actionsToString(adsInsights.getFieldCostPerOutboundClick());
53
+ case "cost_per_thruplay": return actionsToString(adsInsights.getFieldCostPerThruplay());
54
+ case "cost_per_unique_action_type": return actionsToString(adsInsights.getFieldCostPerUniqueActionType());
55
+ case "cost_per_unique_click": return adsInsights.getFieldCostPerUniqueClick();
56
+ case "cost_per_unique_inline_link_click": return adsInsights.getFieldCostPerUniqueInlineLinkClick();
57
+ case "cost_per_unique_outbound_click": return actionsToString(adsInsights.getFieldCostPerUniqueOutboundClick());
58
+ case "cpc": return adsInsights.getFieldCpc();
59
+ case "cpm": return adsInsights.getFieldCpm();
60
+ case "cpp": return adsInsights.getFieldCpp();
61
+ case "ctr": return adsInsights.getFieldCtr();
62
+ case "date_start": return adsInsights.getFieldDateStart();
63
+ case "date_stop": return adsInsights.getFieldDateStop();
64
+ case "deeplink_clicks": return adsInsights.getFieldDeeplinkClicks();
65
+ case "engagement_rate_ranking": return adsInsights.getFieldEngagementRateRanking();
66
+ case "estimated_ad_recall_rate": return adsInsights.getFieldEstimatedAdRecallRate();
67
+ case "estimated_ad_recallers": return adsInsights.getFieldEstimatedAdRecallers();
68
+ case "frequency": return adsInsights.getFieldFrequency();
69
+ case "full_view_impressions": return adsInsights.getFieldFullViewImpressions();
70
+ case "full_view_reach": return adsInsights.getFieldFullViewReach();
71
+ case "impressions": return adsInsights.getFieldImpressions();
72
+ case "inline_link_click_ctr": return adsInsights.getFieldInlineLinkClickCtr();
73
+ case "inline_link_clicks": return adsInsights.getFieldInlineLinkClicks();
74
+ case "inline_post_engagement": return adsInsights.getFieldInlinePostEngagement();
75
+ case "instant_experience_clicks_to_open": return adsInsights.getFieldInstantExperienceClicksToOpen();
76
+ case "instant_experience_clicks_to_start": return adsInsights.getFieldInstantExperienceClicksToStart();
77
+ case "instant_experience_outbound_clicks": return adsInsights.getFieldInstantExperienceOutboundClicks();
78
+ case "mobile_app_purchase_roas": return actionsToString(adsInsights.getFieldMobileAppPurchaseRoas());
79
+ case "newsfeed_avg_position": return adsInsights.getFieldNewsfeedAvgPosition();
80
+ case "newsfeed_clicks": return adsInsights.getFieldNewsfeedClicks();
81
+ case "newsfeed_impressions": return adsInsights.getFieldNewsfeedImpressions();
82
+ case "objective": return adsInsights.getFieldObjective();
83
+ case "outbound_clicks": return actionsToString(adsInsights.getFieldOutboundClicks());
84
+ case "outbound_clicks_ctr": return actionsToString(adsInsights.getFieldOutboundClicksCtr());
85
+ case "purchase_roas": return actionsToString(adsInsights.getFieldPurchaseRoas());
86
+ case "quality_ranking": return adsInsights.getFieldQualityRanking();
87
+ case "reach": return adsInsights.getFieldReach();
88
+ case "relevance_score": return adsInsights.getFieldRelevanceScore().toString();
89
+ case "social_spend": return adsInsights.getFieldSocialSpend();
90
+ case "spend": return adsInsights.getFieldSpend();
91
+ case "unique_actions": return actionsToString(adsInsights.getFieldUniqueActions());
92
+ case "unique_clicks": return adsInsights.getFieldUniqueClicks();
93
+ case "unique_ctr": return adsInsights.getFieldUniqueCtr();
94
+ case "unique_impressions": return adsInsights.getFieldUniqueImpressions();
95
+ case "unique_inline_link_click_ctr": return adsInsights.getFieldUniqueInlineLinkClickCtr();
96
+ case "unique_inline_link_clicks": return adsInsights.getFieldUniqueInlineLinkClicks();
97
+ case "unique_link_clicks_ctr": return adsInsights.getFieldUniqueLinkClicksCtr();
98
+ case "unique_outbound_clicks": return actionsToString(adsInsights.getFieldUniqueOutboundClicks());
99
+ case "unique_outbound_clicks_ctr": return actionsToString(adsInsights.getFieldUniqueOutboundClicksCtr());
100
+ case "video_10_sec_watched_actions": return actionsToString(adsInsights.getFieldVideo10SecWatchedActions());
101
+ case "video_30_sec_watched_actions": return actionsToString(adsInsights.getFieldVideo30SecWatchedActions());
102
+ case "video_avg_time_watched_actions": return actionsToString(adsInsights.getFieldVideoAvgTimeWatchedActions());
103
+ case "video_complete_watched_actions": return actionsToString(adsInsights.getFieldVideoCompleteWatchedActions());
104
+ case "video_p100_watched_actions": return actionsToString(adsInsights.getFieldVideoP100WatchedActions());
105
+ case "video_p25_watched_actions": return actionsToString(adsInsights.getFieldVideoP25WatchedActions());
106
+ case "video_p50_watched_actions": return actionsToString(adsInsights.getFieldVideoP50WatchedActions());
107
+ case "video_p75_watched_actions": return actionsToString(adsInsights.getFieldVideoP75WatchedActions());
108
+ case "video_play_actions": return actionsToString(adsInsights.getFieldVideoPlayActions());
109
+ case "video_thruplay_watched_actions": return actionsToString(adsInsights.getFieldVideoThruplayWatchedActions());
110
+ case "website_clicks": return adsInsights.getFieldWebsiteClicks();
111
+ case "website_ctr": return actionsToString(adsInsights.getFieldWebsiteCtr());
112
+ case "website_purchase_roas": return actionsToString(adsInsights.getFieldWebsitePurchaseRoas());
113
+ // breakdowns
114
+ case "ad_format_asset": return extractBreakdown("ad_format_asset");
115
+ case "age": return extractBreakdown("age");
116
+ case "body_asset": return extractBreakdown("body_asset");
117
+ case "call_to_action_asset": return extractBreakdown("call_to_action_asset");
118
+ case "country": return extractBreakdown("country");
119
+ case "description_asset": return extractBreakdown("description_asset");
120
+ case "gender": return extractBreakdown("gender");
121
+ case "image_asset": return extractBreakdown("image_asset");
122
+ case "impression_device": return extractBreakdown("impression_device");
123
+ case "link_url_asset": return extractBreakdown("link_url_asset");
124
+ case "product_id": return extractBreakdown("product_id");
125
+ case "region": return extractBreakdown("region");
126
+ case "title_asset": return extractBreakdown("title_asset");
127
+ case "video_asset": return extractBreakdown("video_asset");
128
+ case "dma": return extractBreakdown("dma");
129
+ case "frequency_value": return extractBreakdown("frequency_value");
130
+ case "hourly_stats_aggregated_by_advertiser_time_zone": return extractBreakdown("hourly_stats_aggregated_by_advertiser_time_zone");
131
+ case "hourly_stats_aggregated_by_audience_time_zone": return extractBreakdown("hourly_stats_aggregated_by_audience_time_zone");
132
+ case "place_page_id": return extractBreakdown("place_page_id");
133
+ case "publisher_platform": return extractBreakdown("publisher_platform");
134
+ case "platform_position": return extractBreakdown("platform_position");
135
+ case "device_platform": return extractBreakdown("device_platform");
136
+ default: return "";
137
+ }
138
+ }
139
+
140
+ private String actionsToString(List<AdsActionStats> actions)
141
+ {
142
+ return Objects.isNull(actions) ? "[]" : "[" + actions.stream().map(AdsActionStats::toString).collect(Collectors.joining(",")) + "]";
143
+ }
144
+ private String extractBreakdown(String name)
145
+ {
146
+ JsonElement data = this.adsInsights.getRawResponseAsJsonObject().get(name);
147
+ return data.isJsonNull() ? "" : data.toString();
148
+ }
149
+ }
@@ -0,0 +1,243 @@
1
+ package org.embulk.input.facebook_ads_insights;
2
+
3
+ import com.facebook.ads.sdk.APIContext;
4
+ import com.facebook.ads.sdk.APIException;
5
+ import com.facebook.ads.sdk.APINodeList;
6
+ import com.facebook.ads.sdk.Ad;
7
+ import com.facebook.ads.sdk.AdAccount;
8
+ import com.facebook.ads.sdk.AdReportRun;
9
+ import com.facebook.ads.sdk.AdSet;
10
+ import com.facebook.ads.sdk.AdsInsights;
11
+ import com.facebook.ads.sdk.Campaign;
12
+ import org.embulk.input.facebook_ads_insights.model.ActionAttributionWindow;
13
+ import org.embulk.input.facebook_ads_insights.model.ActionBreakdown;
14
+ import org.embulk.input.facebook_ads_insights.model.Breakdown;
15
+ import org.embulk.spi.ColumnConfig;
16
+ import org.slf4j.Logger;
17
+ import org.slf4j.LoggerFactory;
18
+
19
+ import java.util.List;
20
+ import java.util.stream.Collectors;
21
+
22
+ public class Client
23
+ {
24
+ private final Logger logger = LoggerFactory.getLogger(Client.class);
25
+ private static final int ASYNC_SLEEP_TIME = 3000;
26
+
27
+ private final PluginTask pluginTask;
28
+
29
+ public Client(final PluginTask pluginTask)
30
+ {
31
+ this.pluginTask = pluginTask;
32
+ }
33
+
34
+ public APINodeList<AdsInsights> getInsights() throws APIException, InterruptedException
35
+ {
36
+ AdReportRun adReportRun;
37
+ switch (pluginTask.getObjectType()) {
38
+ case ACCOUNT: {
39
+ adReportRun = getAdAccountInsights();
40
+ break;
41
+ }
42
+ case CAMPAIGN: {
43
+ adReportRun = getCampaignInsights();
44
+ break;
45
+ }
46
+ case ADSET: {
47
+ adReportRun = getAdSetInsights();
48
+ break;
49
+ }
50
+ case AD: {
51
+ adReportRun = getAdInsights();
52
+ break;
53
+ }
54
+ default: throw new IllegalArgumentException();
55
+ }
56
+ while (adReportRun.fetch().getFieldAsyncPercentCompletion() != 100) {
57
+ Thread.sleep(ASYNC_SLEEP_TIME);
58
+ }
59
+ return adReportRun.getInsights().execute();
60
+ }
61
+
62
+ // TODO: plz make me dry
63
+ private AdReportRun getAdAccountInsights() throws APIException
64
+ {
65
+ AdAccount.APIRequestGetInsightsAsync request = new AdAccount(pluginTask.getObjectId(), apiContext()).getInsightsAsync();
66
+ request.setFields(fieldNames());
67
+ if (pluginTask.getActionAttributionWindows().isPresent()) {
68
+ request.setActionAttributionWindows(pluginTask.getActionAttributionWindows().get().stream().map(ActionAttributionWindow::getEnum).collect(Collectors.toList()));
69
+ }
70
+ if (pluginTask.getActionBreakdowns().isPresent()) {
71
+ request.setActionBreakdowns(pluginTask.getActionBreakdowns().get().stream().map(ActionBreakdown::getEnum).collect(Collectors.toList()));
72
+ }
73
+ if (pluginTask.getActionReportTime().isPresent()) {
74
+ request.setActionReportTime(pluginTask.getActionReportTime().get().getEnum());
75
+ }
76
+ if (pluginTask.getBreakdowns().isPresent()) {
77
+ request.setBreakdowns(pluginTask.getBreakdowns().get().stream().map(Breakdown::getEnum).collect(Collectors.toList()));
78
+ }
79
+ if (pluginTask.getDatePreset().isPresent()) {
80
+ request.setDatePreset(pluginTask.getDatePreset().get().getEnum());
81
+ }
82
+ if (pluginTask.getFiltering().isPresent()) {
83
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getFiltering().get()));
84
+ }
85
+ if (pluginTask.getLevel().isPresent()) {
86
+ request.setLevel(pluginTask.getLevel().get().getEnum());
87
+ }
88
+ if (pluginTask.getProductIdLimit().isPresent()) {
89
+ request.setProductIdLimit(pluginTask.getProductIdLimit().get());
90
+ }
91
+ if (pluginTask.getSort().isPresent()) {
92
+ request.setSort(pluginTask.getSort().get());
93
+ }
94
+ if (pluginTask.getTimeIncrement().isPresent()) {
95
+ request.setTimeIncrement(pluginTask.getTimeIncrement().get());
96
+ }
97
+ if (pluginTask.getTimeRanges().isPresent()) {
98
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getTimeRanges().get()));
99
+ }
100
+ if (pluginTask.getUseAccountAttributionSetting().isPresent()) {
101
+ request.setUseAccountAttributionSetting(pluginTask.getUseAccountAttributionSetting().get());
102
+ }
103
+ return request.execute();
104
+ }
105
+
106
+ private AdReportRun getCampaignInsights() throws APIException
107
+ {
108
+ Campaign.APIRequestGetInsightsAsync request = new Campaign(pluginTask.getObjectId(), apiContext()).getInsightsAsync();
109
+ request.setFields(fieldNames());
110
+ if (pluginTask.getActionAttributionWindows().isPresent()) {
111
+ request.setActionAttributionWindows(pluginTask.getActionAttributionWindows().get().stream().map(ActionAttributionWindow::getEnum).collect(Collectors.toList()));
112
+ }
113
+ if (pluginTask.getActionBreakdowns().isPresent()) {
114
+ request.setActionBreakdowns(pluginTask.getActionBreakdowns().get().stream().map(ActionBreakdown::getEnum).collect(Collectors.toList()));
115
+ }
116
+ if (pluginTask.getActionReportTime().isPresent()) {
117
+ request.setActionReportTime(pluginTask.getActionReportTime().get().getEnum());
118
+ }
119
+ if (pluginTask.getBreakdowns().isPresent()) {
120
+ request.setBreakdowns(pluginTask.getBreakdowns().get().stream().map(Breakdown::getEnum).collect(Collectors.toList()));
121
+ }
122
+ if (pluginTask.getDatePreset().isPresent()) {
123
+ request.setDatePreset(pluginTask.getDatePreset().get().getEnum());
124
+ }
125
+ if (pluginTask.getFiltering().isPresent()) {
126
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getFiltering().get()));
127
+ }
128
+ if (pluginTask.getLevel().isPresent()) {
129
+ request.setLevel(pluginTask.getLevel().get().getEnum());
130
+ }
131
+ if (pluginTask.getProductIdLimit().isPresent()) {
132
+ request.setProductIdLimit(pluginTask.getProductIdLimit().get());
133
+ }
134
+ if (pluginTask.getSort().isPresent()) {
135
+ request.setSort(pluginTask.getSort().get());
136
+ }
137
+ if (pluginTask.getTimeIncrement().isPresent()) {
138
+ request.setTimeIncrement(pluginTask.getTimeIncrement().get());
139
+ }
140
+ if (pluginTask.getTimeRanges().isPresent()) {
141
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getTimeRanges().get()));
142
+ }
143
+ if (pluginTask.getUseAccountAttributionSetting().isPresent()) {
144
+ request.setUseAccountAttributionSetting(pluginTask.getUseAccountAttributionSetting().get());
145
+ }
146
+ return request.execute();
147
+ }
148
+
149
+ private AdReportRun getAdSetInsights() throws APIException
150
+ {
151
+ AdSet.APIRequestGetInsightsAsync request = new AdSet(pluginTask.getObjectId(), apiContext()).getInsightsAsync();
152
+ request.setFields(fieldNames());
153
+ if (pluginTask.getActionAttributionWindows().isPresent()) {
154
+ request.setActionAttributionWindows(pluginTask.getActionAttributionWindows().get().stream().map(ActionAttributionWindow::getEnum).collect(Collectors.toList()));
155
+ }
156
+ if (pluginTask.getActionBreakdowns().isPresent()) {
157
+ request.setActionBreakdowns(pluginTask.getActionBreakdowns().get().stream().map(ActionBreakdown::getEnum).collect(Collectors.toList()));
158
+ }
159
+ if (pluginTask.getActionReportTime().isPresent()) {
160
+ request.setActionReportTime(pluginTask.getActionReportTime().get().getEnum());
161
+ }
162
+ if (pluginTask.getBreakdowns().isPresent()) {
163
+ request.setBreakdowns(pluginTask.getBreakdowns().get().stream().map(Breakdown::getEnum).collect(Collectors.toList()));
164
+ }
165
+ if (pluginTask.getDatePreset().isPresent()) {
166
+ request.setDatePreset(pluginTask.getDatePreset().get().getEnum());
167
+ }
168
+ if (pluginTask.getFiltering().isPresent()) {
169
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getFiltering().get()));
170
+ }
171
+ if (pluginTask.getLevel().isPresent()) {
172
+ request.setLevel(pluginTask.getLevel().get().getEnum());
173
+ }
174
+ if (pluginTask.getProductIdLimit().isPresent()) {
175
+ request.setProductIdLimit(pluginTask.getProductIdLimit().get());
176
+ }
177
+ if (pluginTask.getSort().isPresent()) {
178
+ request.setSort(pluginTask.getSort().get());
179
+ }
180
+ if (pluginTask.getTimeIncrement().isPresent()) {
181
+ request.setTimeIncrement(pluginTask.getTimeIncrement().get());
182
+ }
183
+ if (pluginTask.getTimeRanges().isPresent()) {
184
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getTimeRanges().get()));
185
+ }
186
+ if (pluginTask.getUseAccountAttributionSetting().isPresent()) {
187
+ request.setUseAccountAttributionSetting(pluginTask.getUseAccountAttributionSetting().get());
188
+ }
189
+ return request.execute();
190
+ }
191
+
192
+ private AdReportRun getAdInsights() throws APIException
193
+ {
194
+ Ad.APIRequestGetInsightsAsync request = new Ad(pluginTask.getObjectId(), apiContext()).getInsightsAsync();
195
+ request.setFields(fieldNames());
196
+ if (pluginTask.getActionAttributionWindows().isPresent()) {
197
+ request.setActionAttributionWindows(pluginTask.getActionAttributionWindows().get().stream().map(ActionAttributionWindow::getEnum).collect(Collectors.toList()));
198
+ }
199
+ if (pluginTask.getActionBreakdowns().isPresent()) {
200
+ request.setActionBreakdowns(pluginTask.getActionBreakdowns().get().stream().map(ActionBreakdown::getEnum).collect(Collectors.toList()));
201
+ }
202
+ if (pluginTask.getActionReportTime().isPresent()) {
203
+ request.setActionReportTime(pluginTask.getActionReportTime().get().getEnum());
204
+ }
205
+ if (pluginTask.getBreakdowns().isPresent()) {
206
+ request.setBreakdowns(pluginTask.getBreakdowns().get().stream().map(Breakdown::getEnum).collect(Collectors.toList()));
207
+ }
208
+ if (pluginTask.getDatePreset().isPresent()) {
209
+ request.setDatePreset(pluginTask.getDatePreset().get().getEnum());
210
+ }
211
+ if (pluginTask.getFiltering().isPresent()) {
212
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getFiltering().get()));
213
+ }
214
+ if (pluginTask.getLevel().isPresent()) {
215
+ request.setLevel(pluginTask.getLevel().get().getEnum());
216
+ }
217
+ if (pluginTask.getProductIdLimit().isPresent()) {
218
+ request.setProductIdLimit(pluginTask.getProductIdLimit().get());
219
+ }
220
+ if (pluginTask.getSort().isPresent()) {
221
+ request.setSort(pluginTask.getSort().get());
222
+ }
223
+ if (pluginTask.getTimeIncrement().isPresent()) {
224
+ request.setTimeIncrement(pluginTask.getTimeIncrement().get());
225
+ }
226
+ if (pluginTask.getTimeRanges().isPresent()) {
227
+ request.setTimeRanges(Util.stringifyToStringMaps(pluginTask.getTimeRanges().get()));
228
+ }
229
+ if (pluginTask.getUseAccountAttributionSetting().isPresent()) {
230
+ request.setUseAccountAttributionSetting(pluginTask.getUseAccountAttributionSetting().get());
231
+ }
232
+ return request.execute();
233
+ }
234
+
235
+ private APIContext apiContext()
236
+ {
237
+ return new APIContext(pluginTask.getAccessToken());
238
+ }
239
+ private List<String> fieldNames()
240
+ {
241
+ return pluginTask.getFields().getColumns().stream().map(ColumnConfig::getName).filter(s -> !Breakdown.NAMES.contains(s)).collect(Collectors.toList());
242
+ }
243
+ }