Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date_preset value error #12

Closed
Alex-git-code opened this issue Apr 21, 2020 · 4 comments
Closed

date_preset value error #12

Alex-git-code opened this issue Apr 21, 2020 · 4 comments
Assignees
Labels
question Further information is requested

Comments

@Alex-git-code
Copy link

Hi @fredericharnois

Many thanks for the code, it will help me a lot.
I have an issue related with the value of date_preset parameter.
I see in the documentation the accepted values are:
today, yesterday, this_month, last_month, this_quarter, lifetime, last_3d, last_7d, last_14d, last_28d, last_30d, last_90d, last_week_mon_sun, last_week_sun_sat, last_quarter, last_year, this_week_mon_today, this_week_sun_today, this_year, but whatever value I use i get the following error:
Exception: Request failed for https://graph.facebook.com returned code 400. Truncated server response: {"error":{"message":"(#100) date_preset is not valid for fields param. please check https://developers.facebook.com/docs/marketing-api/referen... (use muteHttpExceptions option to examine full response). (line 50, file "Code")

My code is:
const TIME_RANGE = 'yesterday';
const facebookUrl = https://graph.facebook.com/v6.0/act_${AD_ACCOUNT_ID}/insights?level=${LEVEL}&fields=${FIELDS}&date_preset=${TIME_RANGE}&access_token=${TOKEN}&time_increment=${TIME_INCREMENT}&limit=5000;

Can you help me with this issue?

Thanks a lot!

@fredericharnois
Copy link
Owner

Hi @alexjs-dev

Glad this project can help out!

Could you give me more information as to what is being passed in FIELDS?

Also, just to be sure, in your actual code, are there backticks (`) around the URL?

Thanks!

@fredericharnois fredericharnois self-assigned this Apr 21, 2020
@fredericharnois fredericharnois added the question Further information is requested label Apr 21, 2020
@Alex-git-code
Copy link
Author

Alex-git-code commented Apr 23, 2020

Hi @fredericharnois ,

Thanks for your reply.
Yes i have backticks (`) around the URL, i don't know why are not displayed here.
Please see my code below(I will not display here the right value for token and ad account id for security reasons):

// ad account ID
const AD_ACCOUNT_ID = '102042577';

// ad, adset, campaign, account
const LEVEL = 'ad';

// https://developers.facebook.com/docs/marketing-api/insights/parameters#fields
const FIELDS = 'campaign_name, adset_name, ad_name, date_preset, reach, impressions, spend, cpm, inline_link_clicks, inline_link_click_ctr, cpc, conversions, conversion_values, cost_per_conversion, purchase_roas, actions';

// https://developers.facebook.com/docs/marketing-api/insights/parameters#param
const TIME_RANGE = 'yesterday';

// user access token linked to a Facebook app
const TOKEN = 'EAAZBMSK3S'

// number of days from 1 to 90
const TIME_INCREMENT = 1;

// https://developers.facebook.com/docs/marketing-api/insights/parameters#param
const FILTERING = [{'field':'action_type','operator':'IN','value':['offsite_conversion.fb_pixel_purchase', 'value' ,'link_click']}]

// DO NOT MODIFY ANYTHING BELOW //

function requestFacebookReport() {

// Builds the Facebook Ads Insights API URL
const facebookUrl = https://graph.facebook.com/v6.0/act_${AD_ACCOUNT_ID}/insights?level=${LEVEL}&fields=${FIELDS}&date_preset=${TIME_RANGE}&access_token=${TOKEN}&time_increment=${TIME_INCREMENT}&limit=5000;
const encodedFacebookUrl = encodeURI(facebookUrl);

const options = {
'method' : 'post'
};

// Fetches & parses the URL
const fetchRequest = UrlFetchApp.fetch(encodedFacebookUrl, options);
const results = JSON.parse(fetchRequest.getContentText());

// Caches the report run ID
const reportId = results.report_run_id;
const cache = CacheService.getScriptCache();
const cached = cache.get('campaign-report-id');

if (cached != null) {
cache.put('campaign-report-id', [], 1);
Utilities.sleep(1001);
cache.put('campaign-report-id', reportId, 21600);
} else {
cache.put('campaign-report-id', reportId, 21600);
};

Logger.log(cache.get('campaign-report-id'));
}

@Alex-git-code
Copy link
Author

I discovered the error, i place date_preset as a field. Thanks anyway. i will close the issue

@pranvirsingh
Copy link

@Alex-git-code Can you please tell me what did you did exactly to make your code work?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants