Examples of creating goals
JS event
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API JS goal",
"type" : "action" ,
"conditions" : [ {
"type" : "exact" ,
"url" : "order"
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Multi-step goal
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Multi-step goal",
"type" : "step" ,
"is_retargeting" : 0,
"steps" : [ {
"name" : "First step of the multi-step goal",
"type" : "url",
"conditions" : [ {
"type" : "contain_action", #Step ID contains
"url" : "stepgoalID"
}
]
},
{
"name" : "Second step of the multistep goal",
"type" : "url",
"conditions" : [ {
"type" : "regexp_action", #Step ID matches a regular expression
"url" : "stepgoalID2"
}
]
},
{
"name" : "Third step of the multi-step goal",
"type" : "url",
"conditions" : [ {
"type" : "action", #Step ID matches
"url" : "stepgoalID3"
}
]
}
]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Page view
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Page view",
"type" : "url" ,
"conditions" : [ {
"type" : "contain" , #url contains
"url" : "mysite.ru/cart"
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Page depth
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Page depth",
"type" : "number" ,
"depth" : 3 #Page depth
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Click on phone number
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Click on phone number",
"type" : "phone" ,
"hide_phone_number": 1, #Hide the phone number (1 - yes, 0 - no)
"conditions" : [ {
"type" : "exact" ,
"url" : "+79990123456"
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Click on email
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Click on email",
"type" : "email" ,
"conditions" : [ {
"type" : "exact" ,
"url" : "mail@mail.ru"
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Open messenger
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Open messenger",
"type" : "messenger" ,
"conditions" : [ {
"type" : "messenger",
"url" : "whatsapp" #Opening WhatsApp
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
File downloads
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API File downloads",
"type" : "file" ,
"conditions" : [ {
"type" : "file",
"url" : "file.pdf" #Filename with or without an extension
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Site search
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Site search",
"type" : "search" ,
"conditions" : [ {
"type" : "search",
"url" : "result" #Name of the GET parameter that accepts a search query on the site
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Click-through to social network
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Click-through to social network",
"type" : "social" ,
"conditions" : [ {
"type" : "social",
"url" : "vkontakte" #Name of the social network
}]
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Transition back from payment system
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Transition back from payment system",
"type" : "payment_system"
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Session duration
import requests
import json
your_token = '<your_token>'
counter_id = <Counter ID>
f={"goal":
{
"name" : "API Session duration",
"type" : "visit_duration",
"duration": 90 #Time in seconds
}
}
headers={f'Authorization': 'OAuth' + your_token}
r = requests.post(url=f'https://api-metrica.yandex.net/management/v1/counter/{counter_id}/goals',
headers=headers,
json=f)
print(json.dumps(r.json(), indent=4))
Was the article helpful?
Previous