Tips and Tricks in a world of Mix

Blog Archives

Image

Ionic3 – Angular4 Super starter change language

download.jpg

 

So I’ve started with Super starter for ionic angular template.

 

The problem was it was only english also it had i18n globalization multi lingual plugin

So I’ve found this and perfected it just a bit.

 

In ..\WebstormProjects\ionic3-angular4\src\pages\tutorial\tutorial.html

Added buttons getting the param of the language wanted :

<ion-header no-shadow>
  <ion-navbar>
    <ion-buttons end >
      <button ion-button (click)="translateTo('he')" color="primary">He</button>
    </ion-buttons>

    <ion-buttons end >
      <button ion-button (click)="translateTo('ru')" color="primary">Ru</button>
    </ion-buttons>

    <ion-buttons end >
      <button ion-button (click)="translateTo('he')" color="primary">He</button>
    </ion-buttons>


    <ion-buttons end *ngIf="showSkip">
      <button ion-button (click)="startApp()" color="primary">{{ 'TUTORIAL_SKIP_BUTTON' | translate}}</button>
    </ion-buttons>


  </ion-navbar>
</ion-header>

 

And in ..\WebstormProjects\ionic3-angular4\src\pages\tutorial\tutorial.ts

I’ve encapsulated the code of  initialization for reuse when the button is hit, so actually all I am doing is reinitializing the language and saying to translate provider want language to use using the parameter I’ve got from clicking the button.

constructor(public navCtrl: NavController, public menu: MenuController, public translate: TranslateService) {

  this.initTranslate();
}

initTranslate(){
  this.translate.get(["TUTORIAL_SLIDE1_TITLE",
    "TUTORIAL_SLIDE1_DESCRIPTION",
    "TUTORIAL_SLIDE2_TITLE",
    "TUTORIAL_SLIDE2_DESCRIPTION",
    "TUTORIAL_SLIDE3_TITLE",
    "TUTORIAL_SLIDE3_DESCRIPTION",
  ]).subscribe(
    (values) => {
      console.log('Loaded values', values);
      this.slides = [
        {
          title: values.TUTORIAL_SLIDE1_TITLE,
          description: values.TUTORIAL_SLIDE1_DESCRIPTION,
          image: 'assets/img/ica-slidebox-img-1.png',
        },
        {
          title: values.TUTORIAL_SLIDE2_TITLE,
          description: values.TUTORIAL_SLIDE2_DESCRIPTION,
          image: 'assets/img/ica-slidebox-img-2.png',
        },
        {
          title: values.TUTORIAL_SLIDE3_TITLE,
          description: values.TUTORIAL_SLIDE3_DESCRIPTION,
          image: 'assets/img/ica-slidebox-img-3.png',
        }
      ];
    });
}

translateTo(lang){
  this.translate.use(lang);
  this.initTranslate();
}

 

That easy. Now you are ready for the global world. 🙂

 

 

 

 

 

 


			
Image

40 Productivity Hacks

Scoro-Infographic-Productivity-Tips.png

Image

Facebook API Marketing connecting through Python SDK

 

face4

Many times we need the data of social and marketing networks available offline or at least off the network itself. For better BI and retrospective of the data we can export all the data from the Facebook , but how?

In this post I am going to summarize the easy way to connect your Facebook marketing accounts to get the data from the facebook.

 

  1. Get https://github.com/facebook/facebook-python-ads-sdk.git
  2. The bootstrapping

Meaning we set our facebook account to use with every function we will use against the facebook api – graph facebook.

my_app_id = '<APP_ID>'
my_app_secret = '<APP_SECRET>'
my_access_token = '<ACCESS_TOKEN>'

But how?

Well , I’ve struggled with it quit a bit.

The app_id and app_secret is easy – you just go to the specific app you want to use and copy it from there.

https://developers.facebook.com/apps/

But how to get the token ?

2.a  https://developers.facebook.com/tools/access_token/  shows you the token

2.b  The more complicated and strong way is through the https://developers.facebook.com/tools/explorer/

Choose an app

.face6a1

Login and allow to get data

face6a

Get user access token

face6

If you don’t have enough permissions facebook will inform you what is needed.

{
  “error”: {
    “message”: “(#298) Requires extended permission: read_mailbox”,

 

3.  Go to pycharm. Add config file as config.json.example inside the example folder of  facebook-python-ads-sdk

{
  "app_id": "<YOUR_APP_ID>",
  "app_secret": "<YOUR_APP_SECRET>",
  "act_id": "act_<YOUR_ACCOUNT_ID>",
  "page_id": "<YOUR_PAGE_ID>",
  "access_token": "<ACCESS_TOKEN>"
}

4. Go to https://business.facebook.com/home/

Choose the account / business you want to work with

business_id== page_id

Go to https://www.facebook.com/ads/manager

face7.PNG

You can see an id of account in the dropbox the act_id or press on the account and see the “act=” value.

Pay attention that you need to add “act_” prefix and then copy paste the id itself.

5. Now inside the Pycharm just right click and debug any of the examples..

 

 

P.S.

Changed

AdCampaign to just Campaign in code

from facebookads.objects import (
    AdUser, Campaign,
    #AdCampaign,
)

 

Code example to get campaigns and insights

 

from facebookads import FacebookSession
from facebookads import FacebookAdsApi
from facebookads.adobjects.adsinsights import AdsInsights
import datetime

from facebookads.objects import (
    AdUser, Campaign,
    #AdCampaign,
)

import json
import os
import pprint

pp = pprint.PrettyPrinter(indent=4)
this_dir = os.path.dirname(__file__)
config_filename = os.path.join(this_dir, 'config.json')

config_file = open(config_filename)
config = json.load(config_file)
config_file.close()

### Setup session and api objects
session = FacebookSession(
    #set4u.biz
    config['app_id'],
    config['app_secret'],
    config['access_token'],
)
api = FacebookAdsApi(session)

if __name__ == '__main__':
    FacebookAdsApi.set_default_api(api)

    print('\n\n\n********** Reading objects example. **********\n')

    ### Setup user and read the object from the server
    me = AdUser(fbid='me')

    ### Read user permissions
    print('>>> Reading permissions field of user:')

    pp.pprint(me.remote_read(fields=[AdUser.Field.permissions]))

    ### Get first account connected to the user
    my_account = me.get_ad_account()

    ### Read connections (in this case, the accounts connected to me)

    # Pro tip: Use list(me.get_ad_accounts()) to make a list out of
    # all the elements out of the iterator

    my_accounts_iterator = me.get_ad_accounts()#[0]
    print('>>> Reading accounts associated with user')
    for account in my_accounts_iterator:
        pp.pprint(account)
        for campaign in account.get_campaigns(fields=[Campaign.Field.name]):
            print(">>> Campaign Stats")
            campaignObj = Campaign(campaign._json[u'id'])
            campaignObj.remote_read(fields=[
                Campaign.Field.name,
                Campaign.Field.effective_status,
                Campaign.Field.objective,
            ])
            pp.pprint(campaignObj);

            today = datetime.date.today()
            start_time = str(today - datetime.timedelta(weeks=1))
            end_time = str(today)

            campaign = Campaign(campaign._json[u'id'])
            params = {
                'time_range': {
                    'since': start_time,
                    'until': end_time,
                },
                'fields': [
                    AdsInsights.Field.impressions,
                    AdsInsights.Field.unique_clicks,
                    AdsInsights.Field.reach,
                    AdsInsights.Field.unique_actions
                ],
            }
            insights = campaign.get_insights(params=params)
            print(insights)

 

This slideshow requires JavaScript.

 

 

 

Tag Cloud

%d bloggers like this: