Examples of creating goals

  1. JS event
  2. Multi-step goal
  3. Page view
  4. Page depth
  5. Click on phone number
  6. Click on email
  7. Open messenger
  8. File downloads
  9. Site search
  10. Click-through to social network
  11. Transition back from payment system

JS event

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <Tag 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
Copied to clipboard

Multi-step goal

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <Tag 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
Copied to clipboard

Page view

import requests
import json

your_token = '<YOUR_TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
   
Copied to clipboard

Page depth

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
Copied to clipboard

Click on phone number

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
  
Copied to clipboard

Click on email

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
      
Copied to clipboard

Open messenger

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
Copied to clipboard

File downloads

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
    
Copied to clipboard

Click-through to social network

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
     
Copied to clipboard

Transition back from payment system

import requests
import json

your_token = '<YOUR TOKEN>'
counter_id = <TAG 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-metrika.yandex.net/management/v1/counter/{counter_id}/goals',
                  headers=headers,
                  json=f)

print(json.dumps(r.json(), indent=4))
Copied to clipboard