|
| 1 | +Usage |
| 2 | +===== |
| 3 | + |
| 4 | +Creating a session |
| 5 | +------------------ |
| 6 | + |
| 7 | +To work with the api you need to get a user from |
| 8 | +`atomx <https://www.atomx.com>`_ and create a |
| 9 | +:class:`atomx.Atomx` session using your email and password: |
| 10 | + |
| 11 | +.. code-block:: python |
| 12 | +
|
| 13 | + from atomx import Atomx |
| 14 | +
|
| 15 | + # create atomx session |
| 16 | + atomx = Atomx('user@example.com', 'password') |
| 17 | +
|
| 18 | +optionally you can specify a different api endpoint for testing: |
| 19 | + |
| 20 | +.. code-block:: python |
| 21 | +
|
| 22 | + # create atomx session with sandbox endpoint |
| 23 | + atomx = Atomx('user@example.com', 'password', |
| 24 | + api_endpoint='https://sandbox.api.atomx.com/v1') |
| 25 | +
|
| 26 | +
|
| 27 | +Fetching resources |
| 28 | +------------------ |
| 29 | + |
| 30 | +Use :meth:`atomx.Atomx.get` to fetch any resource from the api. |
| 31 | +The first parameter is the resource you want to query and can be any |
| 32 | +string that the `atomx api <http://wiki.atomx.com/doku.php?id=api>`_ accepts. |
| 33 | +All additional keyword arguments are used to build the query string. |
| 34 | + |
| 35 | +If the returned resource (or list of resources) is any of the |
| 36 | +:mod:`atomx.models` then that model instance will be returned so |
| 37 | +you can easily work with it. |
| 38 | + |
| 39 | + |
| 40 | +For example to get 10 creatives and then print some information about it: |
| 41 | + |
| 42 | +.. code-block:: python |
| 43 | +
|
| 44 | + # get 10 creatives |
| 45 | + creatives = atomx.get('Creatives', limit=10) |
| 46 | + # the result is a list of `atomx.models.Creative` models |
| 47 | + # that you can easily inspect, manipulate and update |
| 48 | + for creative in creatives: |
| 49 | + print('Creative ID: {c.id}, state: {c.state}, ' |
| 50 | + 'name: {c.name}, title: {c.title}'.format(c=creative)) |
| 51 | +
|
| 52 | +Or query all profiles of advertiser 42 ordered by last updated: |
| 53 | + |
| 54 | +.. code-block:: python |
| 55 | +
|
| 56 | + profiles = atomx.get('advertiser/42/profiles', order_by='updated_at.desc') |
| 57 | +
|
| 58 | +
|
| 59 | +Or get all domains where the hostname contains `atom`: |
| 60 | + |
| 61 | +.. code-block:: python |
| 62 | +
|
| 63 | + domains = atomx.get('domains', name='*atom*') |
| 64 | +
|
| 65 | +
|
| 66 | +Attributes that are not loaded in the model will be lazy loaded once you |
| 67 | +try to access them. |
| 68 | +E.g. if you want to access the `quickstats` for the creative |
| 69 | +we fetched earlier you don't have to to anything special, |
| 70 | +just access the `quickstats` attribute: |
| 71 | + |
| 72 | +.. code-block:: python |
| 73 | +
|
| 74 | + creative = creatives[0] |
| 75 | + print(creative.quickstats) |
| 76 | +
|
| 77 | +Or to get the advertiser for a profile, just: |
| 78 | + |
| 79 | +.. code-block:: python |
| 80 | +
|
| 81 | + advertiser = profiles[0].advertiser |
| 82 | +
|
| 83 | +
|
| 84 | +Updating models |
| 85 | +--------------- |
| 86 | + |
| 87 | +To change a :mod:`atomx.models` model you just change |
| 88 | +any attribute you want and call :meth:`atomx.models.AtomxModel.save`. |
| 89 | + |
| 90 | +E.g. |
| 91 | + |
| 92 | +.. code-block:: python |
| 93 | +
|
| 94 | + # update title for the first creative in list |
| 95 | + creative = creatives[0] |
| 96 | + creative.title = 'shiny new title' |
| 97 | + creative.save() |
| 98 | +
|
| 99 | + # update profile click frequency |
| 100 | + profiles[0].click_frequency_cap_per = 86400 |
| 101 | + profiles[0].save() |
| 102 | +
|
| 103 | +
|
| 104 | +
|
| 105 | +Creating models |
| 106 | +--------------- |
| 107 | + |
| 108 | +To add a new entry in `atomx` just instantiate any :mod:`atomx.models` |
| 109 | +model with all attributes you want your newly created model to have |
| 110 | +and either call :meth:`atomx.models.AtomxModel.create` with your |
| 111 | +:class:`atomx.Atomx` session as parameter or use |
| 112 | +:meth:`atomx.Atomx.save`. |
| 113 | + |
| 114 | +E.g. create a new profile entry: |
| 115 | + |
| 116 | +.. code-block:: python |
| 117 | +
|
| 118 | + # create a new profile |
| 119 | + from atomx.models import Profile |
| 120 | + profile = Profile(advertiser_id=23, name='test profile') |
| 121 | + # Note that you have to pass it a valid `Atomx` session for create |
| 122 | + # or use `atomx.create(profile)` |
| 123 | + profile.create(atomx) |
| 124 | +
|
| 125 | +
|
| 126 | +Search |
| 127 | +------ |
| 128 | + |
| 129 | +Use :meth:`atomx.Atomx.search` to search fast for anything. |
| 130 | + |
| 131 | +:meth:`atomx.Atomx.search` returns a `dict` with all found results for: |
| 132 | +'Advertisers', 'Campaigns', 'Creatives', 'Placements', 'Publishers', 'Sites'. |
| 133 | + |
| 134 | +The resulting :mod:`.models` have only `id` and `name` loaded since that's |
| 135 | +what's returned from the api `/search` call, but attributes will be lazy loaded |
| 136 | +once you try to accessed them. |
| 137 | +Or you can just fetch everything with one api call with :meth:`.AtomxModel.reload`. |
| 138 | + |
| 139 | +Example: |
| 140 | + |
| 141 | +.. code-block:: python |
| 142 | +
|
| 143 | + search_result = atomx.search('atomx') |
| 144 | +
|
| 145 | + campaign = search_result['campaigns'][0] |
| 146 | + assert isinstance(campaign, models.Campaign) |
| 147 | +
|
| 148 | + # campaign has only `id` and `name` loaded but you |
| 149 | + # can still access (lazy load) all attributes |
| 150 | + print(campaign.budget) |
| 151 | + print(campaign.profile) |
| 152 | +
|
| 153 | + # or reload all attributes with one api call |
| 154 | + campaign.reload() |
| 155 | +
|
| 156 | +
|
| 157 | +Reports |
| 158 | +------- |
| 159 | + |
| 160 | +See :meth:`atomx.Atomx.report` for a description of available parameters |
| 161 | +to create a report. |
| 162 | + |
| 163 | +.. code-block:: python |
| 164 | +
|
| 165 | + # reporting example |
| 166 | + # get a report for a specific publisher |
| 167 | + report = atomx.report(scope='publisher', groups=['hour_formatted'], sums=['impressions', 'clicks'], where=[['publisher_id', '==', 42]], from_='2015-02-08 00:00:00', to='2015-02-09 00:00:00', timezone='America/Los_Angeles') |
| 168 | + # check if report is ready |
| 169 | + print(report.is_ready) |
| 170 | + # if pandas is installed you can get the pandas dataframe with `report.pandas` |
| 171 | + # you can also get the report csv in `report.content` without pandas |
| 172 | + df = report.pandas |
| 173 | + # set index to datetime |
| 174 | + import pandas as pd |
| 175 | + df.index = pd.to_datetime(df.pop('hour_formatted')) |
| 176 | + # calculate mean, median, std per hour |
| 177 | + means = df.resample('H', how=['mean', 'median', 'std']) |
| 178 | + # and plot impression and clicks per day |
| 179 | + means['impressions'].plot() |
| 180 | + means['clicks'].plot() |
| 181 | +
|
| 182 | +For more general information about atomx reporting visit the |
| 183 | +`reporting atomx knowledge base entry <https://wiki.atomx.com/doku.php?id=reporting>`_. |
0 commit comments